[英]Python: Indices of all rows in array with non-zero entries only
在一个numpy数组中 ,如何找到仅具有非零条目的所有行的索引 。 例如在数组中:
A = np.array([[ 1, 0, 5],
[25, 2, 0],
[ 7, 8, 9],
[ 0, 0, 4],
[11, 14, 15]])
我想将[2,4]作为输出,因为第2行和第4行是所有条目均为非零的唯一行。
目前,我正在使用
B = A[np.all(A != 0, axis=1)]
获取一个数组,其中所有具有至少一个零的行都将被丢弃。 但是我需要找到索引(即2和4)。
您的方法应该可以进行如下更改:
np.where(np.all(A != 0, axis=1))[0].tolist()
Out[284]: [2, 4]
In [1]: A = np.array([[ 1, 0, 5],
[25, 2, 0],
[ 7, 8, 9],
[ 0, 0, 4],
[11, 14, 15]])
In [2]: Indx1 = np.all(A != 0, axis=1)
print Indx1
Out[2]: Indx1 = [False False True False True]
In [3]: Indx2 = np.where(Indx1==True)
print Indx2
Out[3]: (array([2, 4]),)
In [4]: Indx = A[Indx2[0]]
print Indx
Out[4]: [2 4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.