函数多条件的Numpy

2024-04-26 08:13:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组称为距离的距离。我想选择两个值之间的距离。为此,我编写了以下代码行:

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

但是,这只为条件选择

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

如果我使用一个临时变量按顺序执行命令,它可以正常工作。为什么上面的代码不起作用,我如何让它起作用?

干杯


Tags: and代码距离顺序npwhere条件执行命令
3条回答

我喜欢用np.vectorize来完成这些任务。请考虑以下几点:

>>> # 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.where进行清除输出。但这是你的要求:)

希望有帮助。

您的特定情况下,最好的方法是将您的两个条件更改为一个条件:

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

它只创建一个布尔数组,在我看来更容易阅读,因为它说,drr中的dist(尽管我将r重新定义为您感兴趣区域的中心而不是开始,所以r = r + dr/2.)但这并不能回答您的问题。


问题的答案:
如果您只是试图筛选出不符合您的条件的where元素,那么实际上不需要where

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

因为&将为您提供一个元素级的and(括号是必需的)。

或者,如果出于某种原因确实要使用where,可以执行以下操作:

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

原因:
它不起作用的原因是np.where返回的是索引列表,而不是布尔数组。您正在尝试在两个数字列表之间获取and,这当然没有您期望的True/False值。如果ab都是True值,则a and b返回b。所以说[0,1,2] and [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.where

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

因为公认的答案很好地解释了这个问题。您还可以使用numpy logical functions,它在这里更适合于多种情况:

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

相关问题 更多 >