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