[SciPy-User] save/load scipy sparse csr_matrix in a portable data format

David Warde-Farley wardefar@iro.umontreal...
Mon Jan 23 07:14:22 CST 2012


On 2012-01-21, at 1:34 PM, Dinesh B Vadhia wrote:

> How do you save/load a scipy sparse csr_matrix in a portable format? The sparse matrix is created in Python 3.2.2 (Windows 64-bit) to run on Python 2.7.2 (Linux 64-bit) . I've tried numpy.save and numpy.load as well as scipy.io.mmwrite() and scipy.io.mmread() and none of the methods work.

A csr_matrix really has only 3 data attributes that matter: .data, .indices, and .indptr. They are all simple ndarrays, so numpy.save will work on them. Save those three arrays with numpy.save or numpy.savez, load them back with numpy.load, and then recreate the sparse matrix object with

	new_csr = csr_matrix((data, indices, indptr), shape=(M, N))

Could be smoother but it gets the job done.

David


More information about the SciPy-User mailing list