Created
October 3, 2016 16:46
-
-
Save anyuzx/30bddd9de2a4200b87f1eb0f65f0f0a9 to your computer and use it in GitHub Desktop.
save spicy sparse matrix/array object to file
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
from scipy import sparse, io | |
m = sparse.csr_matrix([[0,0,0],[1,0,0],[0,1,0]]) | |
m # <3x3 sparse matrix of type '<type 'numpy.int64'>' with 2 stored elements in Compressed Sparse Row format> | |
io.mmwrite("test.mtx", m) | |
del m | |
newm = io.mmread("test.mtx") | |
newm # <3x3 sparse matrix of type '<type 'numpy.int32'>' with 2 stored elements in COOrdinate format> | |
newm.tocsr() # <3x3 sparse matrix of type '<type 'numpy.int32'>' with 2 stored elements in Compressed Sparse Row format> | |
newm.toarray() # array([[0, 0, 0], [1, 0, 0], [0, 1, 0]], dtype=int32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment