Created
March 27, 2015 00:15
-
-
Save 4u9u5tsong/8c5a355df430f63740bf to your computer and use it in GitHub Desktop.
python scipy CSR format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Got an answer from the Scipy user group: | |
# http://stackoverflow.com/questions/8955448/save-load-scipy-sparse-csr-matrix-in-portable-data-format | |
# 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)) | |
# So for example: | |
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']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment