I am trying to multiply a sparse matrix with itself using numpy and scipy.sparse.csr_matrix. The size of matrix is 128x256. Its 93% values are 0. Ironically the multiplication using numpy is faster than scipy.sparse. I do not know why? The code I am using is:
import numpy,time
W=numpy.random.choice([0, 1], size=(128,256), p=[0.93,0.07])
start=time.time()
W1=numpy.matmul(W,numpy.transpose(W))
end=time.time()
print(end-start)
from scipy.sparse import csr_matrix
start=time.time()
W1=csr_matrix(W).dot(csr_matrix(W).transpose())
end=time.time()
print(end-start)
Numpy gives time 0.0006 and scipy gives 0.004. Why. Comparing times for dense matrix, numpy gives smaller time on dense matrix as well and scipy takes more time. Why is the time for scipy.sparse not less than numpy for sparse matrix