<p>DaveP的答案是几乎是对的。。。但对于非常稀疏的矩阵可能会导致问题:如果最后一列或行是空的,则会删除它们。因此,为了确保一切正常,还必须存储“shape”属性。</p>
<p>这是我经常使用的代码:</p>
<pre><code>import tables as tb
from numpy import array
from scipy import sparse
def store_sparse_mat(m, name, store='store.h5'):
msg = "This code only works for csr matrices"
assert(m.__class__ == sparse.csr.csr_matrix), msg
with tb.openFile(store,'a') as f:
for par in ('data', 'indices', 'indptr', 'shape'):
full_name = '%s_%s' % (name, par)
try:
n = getattr(f.root, full_name)
n._f_remove()
except AttributeError:
pass
arr = array(getattr(m, par))
atom = tb.Atom.from_dtype(arr.dtype)
ds = f.createCArray(f.root, full_name, atom, arr.shape)
ds[:] = arr
def load_sparse_mat(name, store='store.h5'):
with tb.openFile(store) as f:
pars = []
for par in ('data', 'indices', 'indptr', 'shape'):
pars.append(getattr(f.root, '%s_%s' % (name, par)).read())
m = sparse.csr_matrix(tuple(pars[:3]), shape=pars[3])
return m
</code></pre>
<p>将其应用于csc矩阵是很简单的。</p>