栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

以可移植数据格式保存/加载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'])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号