Save sparse matrix to file python
Save a sparse matrix to a file using Show Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the The sparse matrix to save. Allow compressing the file. Default: True Examples Store sparse matrix to disk, and load it again: >>> import scipy.sparse >>> sparse_matrix = scipy.sparse.csc_matrix(np.array([[0, 0, 3], [4, 0, 0]])) >>> sparse_matrix <2x3 sparse matrix of type ' >>> scipy.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix) >>> sparse_matrix = scipy.sparse.load_npz('/tmp/sparse_matrix.npz') >>> sparse_matrix <2x3 sparse matrix of type ' Here is performance comparison of the three most upvoted answers using Jupyter notebook. The input is a 1M x 100K random sparse matrix with density 0.001, containing 100M non-zero values:
io.mmwrite / io.mmread
(note that the format has been changed from csr to coo). np.savez / np.load
|