pyemma.msm.io.read_matrix¶
-
pyemma.msm.io.
read_matrix
(filename, mode='default', dtype=<type 'float'>, comments='#')¶ Read matrix from ascii file.
Parameters: - filename (str) – Relative or absolute pathname of the input file.
- mode ({'default', 'dense', 'sparse'}) –
How to determine the matrix format
mode ‘default’ Use the filename to determine the format ‘dense’ Reads file as dense matrix ‘sparse’ Reads file as sparse matrix in COO-format - dtype (data-type, optional) – Data-type of the resulting array; default is float.
- comments (str, optional) – The character used to indicate the start of a comment; default: ‘#’.
Returns: A – The stored matrix
Return type: (M, N) ndarray or scipy.sparse matrix
See also
Notes
(M, N) dense matrices are read from ascii files with M rows and N columns. Sparse matrices are read from ascii files in coordinate list (COO) format and converted to sparse matrices in (COO) format.
The dtype and comments options do only apply to reading and writing of ascii files.
Examples
>>> from tempfile import NamedTemporaryFile >>> from pyemma.msm.io import read_matrix, write_matrix
dense
Use temporary file with ending ‘.dat’
>>> tmpfile = NamedTemporaryFile(suffix='.dat')
Dense (3, 2) matrix
>>> A = np.array([[3, 1], [2, 1], [1, 1]]) >>> write_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.dat')
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.]])