0
$\begingroup$

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

$\endgroup$
7
  • $\begingroup$ did you try to increase the size of the matrix and see how the computation time evolves in both cases ? $\endgroup$ Jul 3, 2019 at 15:28
  • $\begingroup$ @RobinNicole replicating the same matrix to get size (384, 256). Numpy gives time: 0.003 and scipy gives 0.01. Time has increased in both. But again numpy gives lesser time than scipy $\endgroup$ Jul 3, 2019 at 15:31
  • $\begingroup$ If I were you I would try it for matrices of size 2^6 x 2^6, 2^7 x 2^7, 2^7 x 2^7, 2^7 x 2^7... until 2^18 x 2^18 for example and you might see that for small size matrices numpy works better than scipy but that when you start to have larg-ish matrices scipy gets better. Could you also provide a piece of code ? $\endgroup$ Jul 3, 2019 at 16:06
  • $\begingroup$ @RobinNicole The piece of code is given in the question. I will try changing sizes of matrices $\endgroup$ Jul 3, 2019 at 16:19
  • $\begingroup$ It s not reproducible code, you do not have any library import or places where you define your matrices $\endgroup$ Jul 3, 2019 at 16:28

1 Answer 1

2
$\begingroup$

Here, you do not time only the time taken to make the matrix multiplication but also the time taken to convert your matrix from dense to sparse. If you convert your matrix before the timing starts, you will see that multiplication with scipy is indeed more than twice faster

import time, numpy, scipy
from scipy.sparse import csr_matrix
import numpy as np

W = np.random.binomial(n=1, p=0.01, size=(100, 100))
start=time.time()
numpy.matmul(W,numpy.transpose(W))
end=time.time()
dt_dense = end - start 
print ('time taken for the dense matrix {}'.format(end - start))

sparse_W = csr_matrix(W)
start=time.time()
sparse_W.dot(sparse_W.transpose())
end=time.time()
dt_sparse = end - start
print ('time taken for the sparse matrix {}'.format(end - start))
dt_dense/dt_sparse
$\endgroup$
5
  • $\begingroup$ Since, I want to compare the overhead of sparse and dense matrices. I would prefer to include csr_matrix conversion within the time itself. $\endgroup$ Jul 3, 2019 at 16:49
  • $\begingroup$ If you put the sparse_W after the timing the scipy will still be slightly faster because you will do the conversion only once instead of twice in the implementation of your question, would it be alright for you. Otherwise could you share what problem you want to solve with this piece of code and maybe I can give you a hand $\endgroup$ Jul 3, 2019 at 16:54
  • $\begingroup$ Yes, as you said it is slightly fast. But I realized my original matrix has floating point entries with 8 digits of precision. Can this be the reason for csr_matrix to give more time for my actual matrix? $\endgroup$ Jul 3, 2019 at 17:18
  • $\begingroup$ Yes I guess float is the problem. Because in your code as well if I change some entries to float. The time for scipy increases $\endgroup$ Jul 3, 2019 at 17:41
  • $\begingroup$ I do not know I can only advise you to try and see $\endgroup$ Jul 4, 2019 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.