简体   繁体   English

查找稀疏csc_matrix中存在非零条目的行的索引

[英]Finding the indices of the rows where there are non-zero entries in a sparse csc_matrix

I have a numpy array, X: 我有一个numpy数组,X:

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

I am interested in finding the indices of the rows where there are non-zero entries, in the 0th column. 我感兴趣的是在第0列中找到存在非零条目的行的索引。 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 a vector that has 0, 2, 5 in it. 这是伟大的,但我要的是具有矢量0, 2, 5在里面。

How do I get the indices I'm looking for? 我如何获得我正在寻找的指数?

Thanks for the help. 谢谢您的帮助。

With a CSC matrix you can do the following: 使用CSC矩阵,您可以执行以下操作:

>>> 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 you what you are after. 所以aa.indices[aa.indptr[col]:aa.indptr[col+1]]应该能让你得到你想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM