ppa*_*ler 21 python arrays numpy clip clamp
我想从列表中替换outliners.因此我定义了上限和下限.现在,上面upper_bound
和下面的每个值lower_bound
都被绑定值替换.我的方法是使用numpy数组分两步完成.
现在我想知道是否可以一步完成,因为我猜它可以提高性能和可读性.
有没有更短的方法来做到这一点?
import numpy as np
lowerBound, upperBound = 3, 7
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[arr > upperBound] = upperBound
arr[arr < lowerBound] = lowerBound
# [3 3 3 3 4 5 6 7 7 7]
print(arr)
Run Code Online (Sandbox Code Playgroud)
art*_*hur 32
你可以使用numpy.clip
:
In [1]: import numpy as np
In [2]: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [3]: lowerBound, upperBound = 3, 7
In [4]: np.clip(arr, lowerBound, upperBound, out=arr)
Out[4]: array([3, 3, 3, 3, 4, 5, 6, 7, 7, 7])
In [5]: arr
Out[5]: array([3, 3, 3, 3, 4, 5, 6, 7, 7, 7])
Run Code Online (Sandbox Code Playgroud)
mat*_*dan 13
对于不依赖的替代方案numpy
,您可以随时使用
arr = [max(lower_bound, min(x, upper_bound)) for x in arr]
Run Code Online (Sandbox Code Playgroud)
如果你只是想设置一个上限,你当然可以写arr = [min(x, upper_bound) for x in arr]
.或者类似地,如果你只想要一个下限,你可以使用max
.
在这里,我刚刚应用了两个操作.
编辑:这是一个稍微深入的解释:
给定x
数组的一个元素(假设你的upper_bound
数量至少与你的一样大lower_bound
!),你将有以下三种情况之一:
一世) x < lower_bound
II) x > upper_bound
iii)lower_bound <= x <= upper_bound
.
在情况(i)中,max/min
表达式首先求值max(lower_bound, x)
,然后解析为lower_bound
.
在情况(ii)中,表达首先变为max(lower_bound, upper_bound)
,然后变为upper_bound
.
在情况(iii)中,我们得到的max(lower_bound, x)
结果只是x
.
在所有三种情况下,输出都是我们想要的.