Skip to content

Instantly share code, notes, and snippets.

@anyuzx
Created October 3, 2016 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anyuzx/30bddd9de2a4200b87f1eb0f65f0f0a9 to your computer and use it in GitHub Desktop.
Save anyuzx/30bddd9de2a4200b87f1eb0f65f0f0a9 to your computer and use it in GitHub Desktop.
save spicy sparse matrix/array object to file
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