以可移植数据格式保存/加载scipy稀疏csr_matrix [英] Save / load scipy sparse csr_matrix in portable data format
问题描述
如何以可移植格式保存/加载scipy稀疏csr_matrix
?稀疏稀疏矩阵是在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 1.19现在具有 scipy.sparse.load_npz
.
edit: SciPy 1.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屋!