What is the fastest way to slice a scipy.sparse matrix?
numpy, python, scipy, sparse-matrix
Solution
To obtain a sparse matrix as output the fastest way to do row slicing is to have a `csr` type, and for columns slicing `csc`, as detailed here. In both cases you just have to do what you are currently doing:
matrix[l1:l2, c1:c2]
If you want a `ndarray` as output it might be faster to perform the slicing directly in the `ndarray` object, which you can obtain from the sparse matrix using the `.A` attribute or the `.toarray()` method:
matrix.A[l1:l2, c1:c2]
or:
matrix.toarray()[l1:l2, c1:c2]
As mentioned in the comment below, converting the sparse array to a dense array might lead to memory errors if the array is big enough.
Problem
I normally use ``` matrix[:, i:] ``` It seems not work as fast as I expected.