我有一个名为dist的距离数组。我想选择一个范围内的dist。

 dists[(np.where(dists >= r)) and (np.where(dists <= r + dr))]

但是,这只选择条件

 (np.where(dists <= r + dr))

如果我使用临时变量按顺序执行命令,它可以很好地工作。为什么上面的代码不能工作,我如何让它工作?


当前回答

要让np.where()在多个条件下工作,只需执行以下操作:

np.where((condition 1) & (condition 2)) # for and
np.where((condition 1) | (condition 2)) # for or

我知道这重复了一些其他的答案,但我把这个简单的答案放在这里给那些仍然疑惑的人,“为什么我收到关于一个包含多个元素的数组的真值是模棱两可的烦人的错误消息”,他们被非常冗长和复杂的回答所迷惑,这些回答解决了原始帖子的一些专门化性质。

现在,至于为什么numpy会在使用and而不是&时中断,我不会在这里回答这个问题。它就是这样:)在这里看到其他答案的解释。在我看来,这似乎是他们应该解决的问题,而不是为了一致性而强迫它。或者至少他们应该给出更好的错误消息。:)

其他回答

在你的特殊情况下,最好的方法就是把你的两个标准改为一个标准:

dists[abs(dists - r - dr/2.) <= dr/2.]

它只创建了一个布尔数组,在我看来更容易阅读,因为它说,dist是dr还是r?(虽然我将重新定义r为感兴趣区域的中心,而不是起点,因此r = r + dr/2。)但这并没有回答你的问题。


你问题的答案是: 如果你只是想过滤掉不符合你的标准的元素,你实际上不需要where:

dists[(dists >= r) & (dists <= r+dr)]

因为&会给你一个elementwise和(括号是必须的)。

或者,如果你出于某种原因想要使用where,你可以这样做:

 dists[(np.where((dists >= r) & (dists <= r + dr)))]

原因: 它不起作用的原因是np。Where返回索引列表,而不是布尔数组。您正在尝试在两个数字列表之间获取和,其中当然没有您所期望的True/False值。如果a和b都是True值,那么a和b返回b。所以说像[0,1,2]和[2,3,4]这样的东西只会给你[2,3,4]。下面是它的实际情况:

In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1

In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)

In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

例如,您希望比较的只是布尔数组

In [236]: dists >= r
Out[236]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True], dtype=bool)

In [237]: dists <= r + dr
Out[237]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

In [238]: (dists >= r) & (dists <= r + dr)
Out[238]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

现在你可以调用np。在组合布尔数组上:

In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)

In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. ,  5.5,  6. ])

或者简单地使用花哨的索引用布尔数组索引原始数组

In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. ,  5.5,  6. ])

要让np.where()在多个条件下工作,只需执行以下操作:

np.where((condition 1) & (condition 2)) # for and
np.where((condition 1) | (condition 2)) # for or

我知道这重复了一些其他的答案,但我把这个简单的答案放在这里给那些仍然疑惑的人,“为什么我收到关于一个包含多个元素的数组的真值是模棱两可的烦人的错误消息”,他们被非常冗长和复杂的回答所迷惑,这些回答解决了原始帖子的一些专门化性质。

现在,至于为什么numpy会在使用and而不是&时中断,我不会在这里回答这个问题。它就是这样:)在这里看到其他答案的解释。在我看来,这似乎是他们应该解决的问题,而不是为了一致性而强迫它。或者至少他们应该给出更好的错误消息。:)

我已经算出了这个简单的例子

import numpy as np

ar = np.array([3,4,5,14,2,4,3,7])

print [X for X in list(ar) if (X >= 3 and X <= 6)]

>>> 
[3, 4, 5, 4, 3]

Try:

import numpy as np
dist = np.array([1,2,3,4,5])
r = 2
dr = 3
np.where(np.logical_and(dist> r, dist<=r+dr))

输出:(数组([2,3,4]),)

您可以查看逻辑函数了解更多详细信息。

Try:

np.intersect1d(np.where(dists >= r)[0],np.where(dists <= r + dr)[0])