以可移植数据格式保存/加载 scipy 稀疏 csr_matrix [英] Save / load scipy sparse csr_matrix in portable data format
问题描述
如何以可移植的格式保存/加载 scipy 稀疏 csr_matrix
?scipy 稀疏矩阵是在 Python 3(Windows 64 位)上创建的,以在 Python 2(Linux 64 位)上运行.最初,我使用了 pickle(协议=2 和 fix_imports=True),但是从 Python 3.2.2(Windows 64 位)到 Python 2.7.2(Windows 32 位)这不起作用并得到错误:
How do you save/load a scipy sparse csr_matrix
in a portable format? The scipy sparse matrix is created on Python 3 (Windows 64-bit) to run on Python 2 (Linux 64-bit). Initially, I used pickle (with protocol=2 and fix_imports=True) but this didn't work going from Python 3.2.2 (Windows 64-bit) to Python 2.7.2 (Windows 32-bit) and got the error:
TypeError: ('data type not understood', <built-in function _reconstruct>, (<type 'numpy.ndarray'>, (0,), '[98]')).
接下来,尝试了 numpy.save
和 numpy.load
以及 scipy.io.mmwrite()
和 scipy.io.mmread()
并且这些方法都不起作用.
Next, tried numpy.save
and numpy.load
as well as scipy.io.mmwrite()
and scipy.io.mmread()
and none of these methods worked either.
推荐答案
scipy 0.19 现在有 scipy.sparse.save_npz
和 scipy.sparse.load_npz
.
edit: scipy 0.19 now has scipy.sparse.save_npz
and scipy.sparse.load_npz
.
from scipy import sparse
sparse.save_npz("yourmatrix.npz", your_matrix)
your_matrix_back = sparse.load_npz("yourmatrix.npz")
对于这两个函数,file
参数也可以是类似文件的对象(即 open
的结果)而不是文件名.
For both functions, the file
argument may also be a file-like object (i.e. the result of open
) instead of a filename.
得到了 Scipy 用户组的答复:
Got an answer from the Scipy user group:
csr_matrix 有 3 个重要的数据属性:.data
、.indices
和 .indptr
.所有都是简单的 ndarrays,所以 numpy.save
可以处理它们.用 numpy.save
或 numpy.savez
保存三个数组,用 numpy.load
加载它们,然后用:
A csr_matrix has 3 data attributes that matter:
.data
,.indices
, and.indptr
. All are simple ndarrays, sonumpy.save
will work on them. Save the three arrays withnumpy.save
ornumpy.savez
, load them back withnumpy.load
, and then recreate the sparse matrix object with:
new_csr = csr_matrix((data, indices, indptr), shape=(M, N))
例如:
def save_sparse_csr(filename, array):
np.savez(filename, data=array.data, indices=array.indices,
indptr=array.indptr, shape=array.shape)
def load_sparse_csr(filename):
loader = np.load(filename)
return csr_matrix((loader['data'], loader['indices'], loader['indptr']),
shape=loader['shape'])
这篇关于以可移植数据格式保存/加载 scipy 稀疏 csr_matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!