numpy.where()详细的逐步说明/示例

问题:numpy.where()详细的逐步说明/示例

numpy.where()尽管阅读了文档这篇文章一篇文章,但我仍然无法正确理解。

有人可以提供有关1D和2D阵列的分步注释示例吗?

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

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


回答 0

摆弄了一段时间后,我发现了问题,并将它们发布在这里,希望对其他人有所帮助。

直观地,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

它也可以用于获取满足条件的数组中的条目:

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

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))

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

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

数组([9])


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

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“.

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

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

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

array([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.


回答 1

这里有点有趣。我发现NumPy通常会完全按照我的意愿去做-有时候,我尝试一些事情比阅读文档要快。实际上两者都是最好的。

我认为您的回答很好(如果愿意,可以接受)。这仅仅是“额外”。

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

给出:

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

…但是:

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

给出:

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

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.

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]