[英]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.