Newer scipy
versions have a scipy.sparse.save_npz
function (and corresponding load). It saves the attributes of a sparse matrix to a numpy
savez
zip archive. In the case of a csr
is saves the data
, indices
and indptr
arrays, plus shape.
scipy.io.savemat
can save a sparse matrix in a MATLAB compatible format (csc). There are one or two other scipy.io
formats that can handle sparse matrices, but I haven't worked with them.
While a sparse matrix contains numpy arrays it isn't an array subclass, so the numpy
functions can't be used directly.
The pickle
method for numpy arrays is its np.save
. And an array that contains objects, uses pickle
(if possible). So a pickle of a dictionary of arrays should work.
The sparse dok
format is a subclass of dict
, so might be pickleable. It might even work with json
. But I haven't tried it.
By the way, a plain numpy array can't be jsoned
either:
In [427]: json.dumps(np.arange(5))
TypeError: array([0, 1, 2, 3, 4]) is not JSON serializable
In [428]: json.dumps(np.arange(5).tolist())
Out[428]: '[0, 1, 2, 3, 4]'
dok
doesn't work either. The keys are tuples of indices,
In [433]: json.dumps(M.todok())
TypeError: keys must be a string
MatrixMarket
is a text format that handles sparse:
In [444]: io.mmwrite('test.mm', M)
In [446]: cat test.mm.mtx
%%MatrixMarket matrix coordinate integer general
%
1 5 4
1 2 1
1 3 2
1 4 3
1 5 4