我想从列表中替换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)
答案 0 :(得分: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])
答案 1 :(得分:13)
对于不依赖numpy
的替代方案,您可以随时
arr = [max(lower_bound, min(x, upper_bound)) for x in arr]
如果你只是想设置一个上限,你当然可以写arr = [min(x, upper_bound) for x in arr]
。或者类似地,如果您只想要下限,则可以使用max
代替。
在这里,我只是同时应用了两个操作。
编辑:这里有一个更深入的解释:
给定数组的x
元素(假设您的upper_bound
至少与lower_bound
一样大!),您将遇到以下三种情况之一:
i)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
。
在所有三种情况下,输出都是我们想要的。