Numpy where 函数多个条件 [英] Numpy where function multiple conditions
问题描述
我有一个称为 dist 的距离数组.我想选择介于两个值之间的 dist.我编写了以下代码行来做到这一点:
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.]
它只创建一个布尔数组,在我看来更容易阅读,因为它说,is 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)]
因为 &
会给你一个元素的 和
(括号是必需的).
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 和b
返回b
.所以说[0,1,2]和[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. ])
或者简单地使用花式索引
In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. , 5.5, 6. ])
这篇关于Numpy where 函数多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!