pyemma.msm.io.load_matrix

pyemma.msm.io.load_matrix(filename, mode='default')

Read matrix from binary file.

Parameters:
  • filename (str) – Relative or absolute pathname of the input file.
  • mode ({'default', 'dense', 'sparse'}) –
    mode  
    ‘default’ Use the filename to determine the matrix formatname.npy (dense), name.coo.npy (sparse)
    ‘dense’ Read file as dense matrix
    ‘sparse’ Read file as sparse matrix in COO-format

See also

save_matrix()

Notes

(M, N) dense matrices are read as ndarray from binary numpy .npy files. Sparse matrices are read as ndarray representing a coordinate list [...,(row, col, value),...] from binary numpy .npy files and returned as sparse matrices in (COO) format.

Examples

>>> from tempfile import NamedTemporaryFile
>>> from pyemma.msm.io import load_matrix, save_matrix

dense

Use temporary file with ending ‘.npy’

>>> tmpfile = NamedTemporaryFile(suffix='.npy')

Dense (3, 2) matrix

>>> A = np.array([[3, 1], [2, 1], [1, 1]])
>>> save_matrix(tmpfile.name, A)

Load from disk

>>> X = load_matrix(tmpfile.name)
>>> X
array([[ 3.,  1.],
       [ 2.,  1.],
       [ 1.,  1.]])

sparse

>>> from scipy.sparse import csr_matrix

Use temporary file with ending ‘.coo.dat’

>>> tmpfile = NamedTemporaryFile(suffix='.coo.npy')

Sparse (3, 3) matrix

>>> A = csr_matrix(np.eye(3))
>>> write_matrix(tmpfile.name, A)

Load from disk

>>> X = load_matrix(tmpfile.name)
>>> X
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])