pyemma.msm.io.save_matrix¶
-
pyemma.msm.io.
save_matrix
(filename, A, mode='default')¶ Save matrix as binary file.
Parameters: - filename (str) – Relative or absolute pathname of the output file.
- A ((M, N) ndarray or sparse matrix) –
- mode ({'default', 'dense', 'sparse'}) –
mode ‘default’ Use the type of A to determine the formatname.xxx (dense), name.coo.xxx (sparse) ‘dense’ Enforce conversion to a dense representationand store the corresponding ndarray ‘sparse’ Convert to sparse matrix in COO-formatand store the coordinate list as ndarray
See also
Notes
(M, N) dense matrices are stored as ndarrays in numpy .npy binary format. Sparse matrices are converted to coordinate list (COO) format. The coordinate list [...,(row, col, value),...] is then stored as a (K, 3) ndarray in numpy .npy binary format. K is the number of nonzero entries in the sparse matrix.
Using the naming scheme name.npy for dense matrices and name.coo.npy for sparse matrices will allow load_matrix to automatically infer the appropriate matrix type from the given filename.
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]]) >>> 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.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.]])