以可移植数据格式保存加载scipy稀疏csr_matrix

以可移植数据格式保存加载scipy稀疏csr_matrix,第1张

以可移植数据格式保存/加载scipy稀疏csr_matrix

编辑: SciPy
1.19现在具有

scipy.sparse.save_npz
scipy.sparse.load_npz

from scipy import sparsesparse.save_npz("yourmatrix.npz", your_matrix)your_matrix_back = sparse.load_npz("yourmatrix.npz")

对于这两个函数,

file
参数也可以是类似于文件的对象(即的结果
open
),而不是文件名。


从Scipy用户组得到了答案:

一个csr_matrix有3个数据属性此事:

.data
.indices
,和
.indptr
。所有都是简单的ndarray,因此
numpy.save
可以在它们上使用。用
numpy.save
或保存三个数组,用
numpy.savez
加载它们
numpy.load
,然后用以下方法重新创建稀疏矩阵对象:

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'])


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5650572.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存