I have a scipy sparse CSR
matrix of size 2M x 50k with 200M non-zero values (100 per row). I need to slice 120k rows of it by a (randomly distributed) index (which is a pandas Series
) and then multiply that submatrix by a sparse vector of size 1x50k (with 100 non-zero values as well).
I do this:
slice = matrix[index.tolist(), :]
result = slice.dot(vector.T).T.toarray()[0] # returns 1x120k array
The slicing takes 0.7s
(slow) and then multiplication takes 0.05s
.
Instead, I can multiply the whole matrix first and then slice the result:
result = matrix.dot(vector.T).T.toarray()[0]
result_sliced = result[index.tolist()] # returns 1x120k array
In this case, multiplication takes 0.65s
and then slicing takes 0.015s
.
Questions:
Why is slicing of a CSR matrix by rows so slow? Even multiplication of the whole matrix takes less time than it.
Is there a way to achieve the end result faster?