脾气暴躁的地方有多个条件 [英] Numpy where function multiple conditions
问题描述
我有一个称为dists的距离数组.我想选择两个值之间的距离.我编写了以下代码来做到这一点:
I have an array of distances called dists. I want to select dists which are between two values. I wrote the following line of code to do that:
dists[(np.where(dists >= r)) and (np.where(dists <= r + dr))]
但这仅针对条件选择
(np.where(dists <= r + dr))
如果我使用临时变量按顺序执行命令,则可以正常工作.上面的代码为什么不起作用,如何使它起作用?
If I do the commands sequentially by using a temporary variable it works fine. Why does the above code not work, and how do I get it to work?
欢呼
推荐答案
在您的特定情况下的最佳方法是将两个条件更改为一个条件:
The best way in your particular case would just be to change your two criteria to one criterion:
dists[abs(dists - r - dr/2.) <= dr/2.]
它只会创建一个布尔数组,在我看来更容易阅读,因为它说是dist
在dr
还是r
内?(尽管我会重新定义r
成为您感兴趣区域的中心,而不是起点,所以r = r + dr/2.
)但这不能回答您的问题.
It only creates one boolean array, and in my opinion is easier to read because it says, is dist
within a dr
or r
? (Though I'd redefine r
to be the center of your region of interest instead of the beginning, so r = r + dr/2.
) But that doesn't answer your question.
问题的答案:
如果您只是想过滤出不符合条件的dists
元素,则实际上并不需要where
:
The answer to your question:
You don't actually need where
if you're just trying to filter out the elements of dists
that don't fit your criteria:
dists[(dists >= r) & (dists <= r+dr)]
因为&
将为您提供元素级的and
(必须带括号).
Because the &
will give you an elementwise and
(the parentheses are necessary).
或者,如果您出于某些原因确实想使用where
,则可以执行以下操作:
Or, if you do want to use where
for some reason, you can do:
dists[(np.where((dists >= r) & (dists <= r + dr)))]
原因:
它不起作用的原因是因为np.where
返回的是索引列表,而不是布尔数组.您试图在两个数字列表之间获得and
,而这些数字当然没有您期望的True
/False
值.如果a
和b
均为True
值,则a and b
返回b
.所以说类似[0,1,2] and [2,3,4]
这样的话只会给你[2,3,4]
.它在起作用:
Why:
The reason it doesn't work is because np.where
returns a list of indices, not a boolean array. You're trying to get and
between two lists of numbers, which of course doesn't have the True
/False
values that you expect. If a
and b
are both True
values, then a and b
returns b
. So saying something like [0,1,2] and [2,3,4]
will just give you [2,3,4]
. Here it is in action:
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]),)
例如,您希望比较的只是布尔数组
What you were expecting to compare was simply the boolean array, for example
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.where
了:
Now you can call np.where
on the combined boolean array:
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. ])
或者使用 fancy indexing
In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. , 5.5, 6. ])
这篇关于脾气暴躁的地方有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!