numpy.where()详细的,逐步的解释/示例

时间:2022-06-03 16:33:30

I have trouble properly understanding numpy.where() despite reading the doc, this post and this other post.

我无法正确理解numpy.where()尽管阅读了文档,这篇文章和其他帖子。

Can someone provide step-by-step commented examples with 1D and 2D arrays?

有人可以提供一维和二维阵列的逐步注释示例吗?

2 个解决方案

#1


126  

After fiddling around for a while, I figured things out, and am posting them here hoping it will help others.

在摆弄了一段时间后,我想出了一些事情,并将它们发布在这里,希望它能帮助别人。

Intuitively, np.where is like asking "tell me where in this array, entries satisfy a given condition".

直觉上,np.where就像问“告诉我在这个数组中哪些条目满足给定条件”。

>>> a = np.arange(5,10)
>>> np.where(a < 8)       # tell me where in a, entries are < 8
(array([0, 1, 2]),)       # answer: entries indexed by 0, 1, 2

It can also be used to get entries in array that satisfy the condition:

它还可用于获取满足条件的数组条目:

>>> a[np.where(a < 8)] 
array([5, 6, 7])          # selects from a entries 0, 1, 2

When a is a 2d array, np.where() returns an array of row idx's, and an array of col idx's:

当a是2d数组时,np.where()返回一个行idx的数组,以及一个col idx的数组:

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

So that as in the 1d case, we can use np.where() to get entries in the 2d array that satisfy the condition:

因此,与1d情况一样,我们可以使用np.where()来获取满足条件的2d数组中的条目:

>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2

array([9])

阵列([9])


Note, when a is 1d, np.where() still returns an array of row idx's and an array of col idx's but columns are of length 1, so latter is empty array.

注意,当a为1d时,np.where()仍返回行idx的数组和col idx的数组,但列的长度为1,因此后者为空数组。

#2


4  

Here is a little more fun. I've found that very often NumPy does exactly what I wish it would do - sometimes it's faster for me to just try things than it is to read the docs. Actually a mixture of both is best.

这里更有趣。我发现NumPy经常完全按照我的意愿行事 - 有时我尝试的东西比阅读文档更快。实际上两者的混合是最好的。

I think your answer is fine (and it's OK to accept it if you like). This is just "extra".

我认为你的答案很好(如果你愿意,可以接受它)。这只是“额外的”。

import numpy as np

a = np.arange(4,10).reshape(2,3)

wh = np.where(a>7)
gt = a>7
x  = np.where(gt)

print "wh: ", wh
print "gt: ", gt
print "x:  ", x

gives:

得到:

wh:  (array([1, 1]), array([1, 2]))
gt:  [[False False False]
      [False  True  True]]
x:   (array([1, 1]), array([1, 2]))

... but:

......但是:

print "a[wh]: ", a[wh]
print "a[gt]  ", a[gt]
print "a[x]:  ", a[x]

gives:

得到:

a[wh]:  [8 9]
a[gt]   [8 9]
a[x]:   [8 9]

#1


126  

After fiddling around for a while, I figured things out, and am posting them here hoping it will help others.

在摆弄了一段时间后,我想出了一些事情,并将它们发布在这里,希望它能帮助别人。

Intuitively, np.where is like asking "tell me where in this array, entries satisfy a given condition".

直觉上,np.where就像问“告诉我在这个数组中哪些条目满足给定条件”。

>>> a = np.arange(5,10)
>>> np.where(a < 8)       # tell me where in a, entries are < 8
(array([0, 1, 2]),)       # answer: entries indexed by 0, 1, 2

It can also be used to get entries in array that satisfy the condition:

它还可用于获取满足条件的数组条目:

>>> a[np.where(a < 8)] 
array([5, 6, 7])          # selects from a entries 0, 1, 2

When a is a 2d array, np.where() returns an array of row idx's, and an array of col idx's:

当a是2d数组时,np.where()返回一个行idx的数组,以及一个col idx的数组:

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

So that as in the 1d case, we can use np.where() to get entries in the 2d array that satisfy the condition:

因此,与1d情况一样,我们可以使用np.where()来获取满足条件的2d数组中的条目:

>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2

array([9])

阵列([9])


Note, when a is 1d, np.where() still returns an array of row idx's and an array of col idx's but columns are of length 1, so latter is empty array.

注意,当a为1d时,np.where()仍返回行idx的数组和col idx的数组,但列的长度为1,因此后者为空数组。

#2


4  

Here is a little more fun. I've found that very often NumPy does exactly what I wish it would do - sometimes it's faster for me to just try things than it is to read the docs. Actually a mixture of both is best.

这里更有趣。我发现NumPy经常完全按照我的意愿行事 - 有时我尝试的东西比阅读文档更快。实际上两者的混合是最好的。

I think your answer is fine (and it's OK to accept it if you like). This is just "extra".

我认为你的答案很好(如果你愿意,可以接受它)。这只是“额外的”。

import numpy as np

a = np.arange(4,10).reshape(2,3)

wh = np.where(a>7)
gt = a>7
x  = np.where(gt)

print "wh: ", wh
print "gt: ", gt
print "x:  ", x

gives:

得到:

wh:  (array([1, 1]), array([1, 2]))
gt:  [[False False False]
      [False  True  True]]
x:   (array([1, 1]), array([1, 2]))

... but:

......但是:

print "a[wh]: ", a[wh]
print "a[gt]  ", a[gt]
print "a[x]:  ", a[x]

gives:

得到:

a[wh]:  [8 9]
a[gt]   [8 9]
a[x]:   [8 9]