I have an scipy CSR matrix and i want to get element column indices for each row. My approach is:
import scipy.sparse as sp
N = 100
d = 0.1
M = sp.rand(N, N, d, format='csr')
indM = [row.nonzero()[1] for row in M]
indM is what i need, it has the same number of row as M and looks like this:
[array([ 6, 7, 11, ..., 79, 85, 86]),
array([12, 20, 25, ..., 84, 93, 95]),
...
array([ 7, 24, 32, 40, 50, 51, 57, 71, 74, 96]),
array([ 1, 4, 9, ..., 71, 95, 96])]
The problem is that with big matrices this approach looks slow. Is there any way to avoid list comprehension or somehow speed this up?
Thank you.