简体   繁体   English

获取非null稀疏csr矩阵行的索引

[英]Get indices of non null scipy sparse csr matrix rows

I want to get the indices of the rows of a scipy.sparse.csr_matrix that are not null. 我想获取不为null的scipy.sparse.csr_matrix的行的索引。 For example: 例如:

A = [ 0 0 0 0 0 1
      0 0 0 0 0 0 
      1 1 0 0 0 0 ]

Desired output: 所需的输出:

indices = [0, 2]

Code: 码:

from scipy.sparse import find, csr_matrix
import numpy as np

A = csr_matrix(np.array([[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0]]))
print(A.todense())

nnz_per_row = A.getnnz(axis=1)
result = np.where(nnz_per_row > 0)[0]

print(result)

Output: 输出:

[[0 0 0 0 0 1]
 [0 0 0 0 0 0]
 [1 1 0 0 0 0]]
[0 2]

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

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