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

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

但是,这只选择条件

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

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


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

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

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

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:

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

公认的答案很好地解释了这个问题。但是,应用多个条件的更Numpythonic方法是使用numpy逻辑函数。在这种情况下,您可以使用np.logical_and:

np.where(np.logical_and(np.greater_equal(dists,r),np.greater_equal(dists,r + dr)))

我喜欢用np。向量化这类任务。考虑以下几点:

>>> # function which returns True when constraints are satisfied.
>>> func = lambda d: d >= r and d<= (r+dr) 
>>>
>>> # Apply constraints element-wise to the dists array.
>>> result = np.vectorize(func)(dists) 
>>>
>>> result = np.where(result) # Get output.

你也可以用np。Argwhere而不是np。其中为明确的输出。


这应该可以工作:

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

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

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


这里有一件有趣的事;通常使用OR和and的方式在这种情况下也适用,但有一个小的变化。而不是“and”和“or”,而是使用&号(&)和管道运算符(|),这样就可以了。

当我们使用'and'时:

ar = np.array([3,4,5,14,2,4,3,7])
np.where((ar>3) and (ar<6), 'yo', ar)

Output:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

当我们使用&号时:

ar = np.array([3,4,5,14,2,4,3,7])
np.where((ar>3) & (ar<6), 'yo', ar)

Output:
array(['3', 'yo', 'yo', '14', '2', 'yo', '3', '7'], dtype='<U11')

当我们在pandas数据框架中应用多个过滤器时,情况也是如此。现在,这背后的原因必须与逻辑运算符和位运算符做一些事情,为了更好地理解相同,我建议在stackoverflow中通过这个答案或类似的Q/A。

更新

一个用户问,为什么括号内需要给出(ar>3)和(ar<6)。事情是这样的。在我开始讨论这里发生的事情之前,需要了解Python中的操作符优先级。

与BODMAS类似,python也会优先考虑应该首先执行的操作。括号内的项首先执行,然后按位操作符开始工作。下面我将展示在使用和不使用"(",")"的两种情况下发生的情况。

Case1:

np.where( ar>3 & ar<6, 'yo', ar)
np.where( np.array([3,4,5,14,2,4,3,7])>3 & np.array([3,4,5,14,2,4,3,7])<6, 'yo', ar)

由于这里没有括号,位操作符(&)在这里会被弄糊涂,你甚至会让它获得逻辑与,因为在操作符优先级表中,&的优先级高于<或>操作符。这是从最低优先级到最高优先级的表格。

它甚至没有执行<和>操作,而是被要求执行逻辑与操作。这就是它给出错误的原因。

你可以点击下面的链接了解更多:操作员 优先级

现在来看情况2:

如果使用括号,可以清楚地看到发生了什么。

np.where( (ar>3) & (ar<6), 'yo', ar)
np.where( (array([False,  True,  True,  True, False,  True, False,  True])) & (array([ True,  True,  True, False,  True,  True,  True, False])), 'yo', ar)

True和False的两个数组。并且可以很容易地对它们进行逻辑与操作。这就给了你:

np.where( array([False,  True,  True, False, False,  True, False, False]),  'yo', ar)

剩下的,np。其中,对于给定的情况,只要True,赋第一个值(即。这里是'yo'),如果为False,则另一个(即。这里,保留原件)。

这是所有。我希望我很好地解释了这个问题。


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

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

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

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