我想要获取一个 scipy.sparse.csr_matrix
中非空行的索引。例如:
A = [ 0 0 0 0 0 1
0 0 0 0 0 0
1 1 0 0 0 0 ]
期望输出:
indices = [0, 2]
我想要获取一个 scipy.sparse.csr_matrix
中非空行的索引。例如:
A = [ 0 0 0 0 0 1
0 0 0 0 0 0
1 1 0 0 0 0 ]
期望输出:
indices = [0, 2]
代码:
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)
输出:
[[0 0 0 0 0 1]
[0 0 0 0 0 0]
[1 1 0 0 0 0]]
[0 2]