您的位置:首页 > 其它

Save / load scipy array,sparse csr_matrix

2016-02-23 09:58 405 查看
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']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: