Python numpy 获取值为 true 的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16094563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:45:06  来源:igfitidea点击:

numpy get index where value is true

pythonnumpymatrix

提问by change

>>> ex=np.arange(30)
>>> e=np.reshape(ex,[3,10])
>>> e
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
>>> e>15
array([[False, False, False, False, False, False, False, False, False,
        False],
       [False, False, False, False, False, False,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True]], dtype=bool)

I need to find the rows that have true or rows in ewhose value are more than 15. I could iterate using a for loop, however, I would like to know if there is a way numpy could do this more efficiently?

我需要找到具有真e值的行或值大于 15 的行。我可以使用 for 循环进行迭代,但是,我想知道 numpy 是否可以更有效地执行此操作?

采纳答案by Jaime

To get the row numbers where at least one item is larger than 15:

要获取至少一项大于 15 的行号:

>>> np.where(np.any(e>15, axis=1))
(array([1, 2], dtype=int64),)

回答by Hadi

You can use nonzerofunction. it returns the nonzero indices of the given input.

您可以使用非零函数。它返回给定输入的非零索引。

Easy Way

简单的方法

>>> (e > 15).nonzero()

(array([1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), array([6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))

to see the indices more cleaner, use transposemethod:

要更清晰地查看索引,请使用transpose方法:

>>> numpy.transpose((e>15).nonzero())

[[1 6]
 [1 7]
 [1 8]
 [1 9]
 [2 0]
 ...

Not Bad Way

不错的方式

>>> numpy.nonzero(e > 15)

(array([1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), array([6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))

or the clean way:

或干净的方式:

>>> numpy.transpose(numpy.nonzero(e > 15))

[[1 6]
 [1 7]
 [1 8]
 [1 9]
 [2 0]
 ...

回答by Opt

A simple and clean way:use np.argwhereto group the indices by element, rather than dimension as in np.nonzero(a)(i.e., np.argwherereturns a row for each non-zero element).

一种简单而干净的方法:使用np.argwhere按元素而不是维度对索引进行分组np.nonzero(a)(即,np.argwhere为每个非零元素返回一行)。

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.argwhere(a>4)
array([[5],
       [6],
       [7],
       [8],
       [9]])

np.argwhere(a)is the same as np.transpose(np.nonzero(a)).

np.argwhere(a)与 相同np.transpose(np.nonzero(a))

Note:You cannot use a(np.argwhere(a>4))to get the corresponding values in a. The recommended way is to use a[(a>4).astype(bool)]or a[(a>4) != 0]rather than a[np.nonzero(a>4)]as they handle 0-d arrays correctly. See the documentationfor more details. As can be seen in the following example, a[(a>4).astype(bool)]and a[(a>4) != 0]can be simplified to a[a>4].

注意:您不能使用a(np.argwhere(a>4))来获取a. 推荐的方法是使用a[(a>4).astype(bool)]ora[(a>4) != 0]而不是a[np.nonzero(a>4)]因为它们正确处理 0-d 数组。有关更多详细信息,请参阅文档。如在下面的例子中可以看出,a[(a>4).astype(bool)]并且a[(a>4) != 0]可以被简化为a[a>4]

Another example:

另一个例子:

>>> a = np.array([5,-15,-8,-5,10])
>>> a
array([  5, -15,  -8,  -5,  10])
>>> a > 4
array([ True, False, False, False,  True])
>>> a[a > 4]
array([ 5, 10])
>>> a = np.add.outer(a,a)
>>> a
array([[ 10, -10,  -3,   0,  15],
       [-10, -30, -23, -20,  -5],
       [ -3, -23, -16, -13,   2],
       [  0, -20, -13, -10,   5],
       [ 15,  -5,   2,   5,  20]])
>>> a = np.argwhere(a>4)
>>> a
array([[0, 0],
       [0, 4],
       [3, 4],
       [4, 0],
       [4, 3],
       [4, 4]])
>>> [print(i,j) for i,j in a]
0 0
0 4
3 4
4 0
4 3
4 4