Created
February 18, 2012 08:34
-
-
Save fannix/1858252 to your computer and use it in GitHub Desktop.
Revisions
-
fannix revised this gist
Feb 18, 2012 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ """http://stackoverflow.com/questions/6282432/load-sparse-array-from-npy-file """ import random import scipy.sparse as sparse import scipy.io -
fannix revised this gist
Feb 18, 2012 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,17 +3,17 @@ import scipy.io import numpy as np def save_sparse_matrix(filename, x): x_coo = x.tocoo() row = x_coo.row col = x_coo.col data = x_coo.data shape = x_coo.shape np.savez(filename, row=row, col=col, data=data, shape=shape) def load_sparse_matrix(filename): y = np.load(filename) z = sparse.coo_matrix((y['data'], (y['row'], y['col'])), shape=y['shape']) return z N=20000 -
fannix created this gist
Feb 18, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ import random import scipy.sparse as sparse import scipy.io import numpy as np def save_sparse_matrix(filename,x): x_coo=x.tocoo() row=x_coo.row col=x_coo.col data=x_coo.data shape=x_coo.shape np.savez(filename,row=row,col=col,data=data,shape=shape) def load_sparse_matrix(filename): y=np.load(filename) z=sparse.coo_matrix((y['data'],(y['row'],y['col'])),shape=y['shape']) return z N=20000 x = sparse.lil_matrix( (N,N) ) for i in xrange(N): x[random.randint(0,N-1),random.randint(0,N-1)]=random.randint(1,100) save_sparse_matrix('/tmp/my_array',x) load_sparse_matrix('/tmp/my_array.npz').tolil()