You can use scipy
to create sparse matrices from dense numpy arrays that only store values with nonzero entries against their indices.
import scipy
import pickle
I = np.eye(10000) #Had 10000 nonzero values along diagonal
S = scipy.sparse.csr_matrix(I)
S
<10000x10000 sparse matrix of type '<class 'numpy.float64'>'
with 10000 stored elements in Compressed Sparse Row format>
This is highly memory efficient and you can use pickle
to dump / load this sparse matrix when you need it.
#Pickle dump
file = open("S.pickle",'wb') #160kb
pickle.dump(S, file)
#Pickle load
file = open("S.pickle",'rb')
S = pickle.load(file)
To get back a dense representation you can simply use .toarray()
to get back a NumPy array or .todense()
to get back a matrix type object.
S.toarray()
array([[1., 0., 0., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]])