簡體   English   中英

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

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

我有一個numpy數組,X:

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

我感興趣的是在第0列中找到存在非零條目的行的索引。 我試過了:

getcol =  X.getcol(0)
print getcol

這給了我:

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

這是偉大的,但我要的是具有矢量0, 2, 5在里面。

我如何獲得我正在尋找的指數?

謝謝您的幫助。

使用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])

所以aa.indices[aa.indptr[col]:aa.indptr[col+1]]應該能讓你得到你想要的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM