Search for row indices where there are nonzero entries in sparse csc_matrix

I have a numpy array, X:

type(X)
>>> <class 'scipy.sparse.csc.csc_matrix'>

I'm interested in finding the indices of rows that have non-zero entries in the 0th column. I tried:

getcol =  X.getcol(0)
print getcol

which gives me:

(0, 0)  1
(2, 0)  1
(5, 0)  10

This is great, but what I want is the vector that has 0, 2, 5it.

How to get the indexes I'm looking for?

Thanks for the help.

+4
source share
1 answer

Using a CSC matrix, you can do the following:

>>> import scipy.sparse as sps
>>> a = np.array([[1, 0, 0],
...               [0, 1, 0],
...               [1, 0, 1],
...               [0, 0, 1],
...               [0, 1, 0],
...               [1, 0, 1]])
>>> aa = sps.csc_matrix(a)
>>> aa.indices[aa.indptr[0]:aa.indptr[1]]
array([0, 2, 5])
>>> aa.indices[aa.indptr[1]:aa.indptr[2]]
array([1, 4])
>>> aa.indices[aa.indptr[2]:aa.indptr[3]]
array([2, 3, 5])

So, aa.indices[aa.indptr[col]:aa.indptr[col+1]]should get what you need.

+3
source

Source: https://habr.com/ru/post/1536403/


All Articles