[SciPy-User] saving a sparse matrix to file

Pengkui Luo pengkui.luo@gmail....
Mon Jun 25 13:34:11 CDT 2012

Is there a unified API to save a sparse matrix of any type (dok, lil, csc,
...) to file, as simple as pickle dump/load?

e.g. Can I do something like the following?

import numpy as np
from scipy.sparse import dok_matrix
from scipy.io import save, load
mat = dok_matrix (np.zeros ((80000, 80000)), np.int)
save (file ('foobar.mat', 'wb'), mat)  # or mat.save ('foobar.mat')
mat2 = load (file ('foobar.mat'))

It seems that there was a .save method for a sparse matrix several years
ago, but was later deprecated. Why?

I came across this email response stating that for csr_matrix one can
save.data, .indices, and .indptr attributes individually to three different
files and load them all to reassemble the matrix. Is it a hack only
suitable for csr_matrix, or one that can be applied to all types of sparse
matrices? Can I save a matrix to a single file instead of three?

I also came across the scipy.io.savemat API, but had no idea how to use
it.... What does "mdict : Dictionary from which to save matfile variables."
mean? What is this dict for? Can someone provide an example on how to use

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

More information about the SciPy-User mailing list