编辑: 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'])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)