A csr_matrix has 3 data attributes that matter: .data, .indices,
and .indptr.
All are simple ndarrays, so numpy.save will
work on them. Save the three arrays with numpy.save or numpy.savez,
load them back with numpy.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(npzfilename):
loader = np.load(npzfilename)
return csr_matrix(( loader['data'], loader['indices'], loader['indptr']),shape = loader['shape'])
def save_ids_array(filename,array):
np.savez(filename+'.ids',data = array)
def load_ids_array(npzfilename):
loader = np.load(npzfilename)
return loader['data']
本文介绍如何使用NumPy保存和加载CSR格式的稀疏矩阵,并提供了具体的方法实现。通过将CSR矩阵的三个关键属性:.data, .indices 和 .indptr 分别保存为NumPy数组文件,可以有效地进行稀疏矩阵的持久化存储。
611

被折叠的 条评论
为什么被折叠?



