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

Dinesh B Vadhia dineshbvadhia@hotmail....
Mon Jan 23 13:26:32 CST 2012

Ah, thank-you David.  Didn't see any doc mentioning re-creating the sparse matrix.  Brill!

Date: Mon, 23 Jan 2012 08:14:22 -0500
From: David Warde-Farley <wardefar@iro.umontreal.ca>
Subject: Re: [SciPy-User] save/load scipy sparse csr_matrix in a
portable data format
To: SciPy Users List <scipy-user@scipy.org>
Message-ID: <B5751591-4FA4-4F53-8D6C-7F96BAF9856B@iro.umontreal.ca>
Content-Type: text/plain; charset=us-ascii

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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120123/26549940/attachment.html 

More information about the SciPy-User mailing list