我读到 numpy 在舍入方面是无偏的,并且它按照其设计的方式工作。 “如果你总是将 0.5 向上舍入到下一个最大数字,那么一堆四舍五入数字的平均值可能会略大于未四舍五入数字的平均值:这种偏差或漂移可能会对某些数值算法产生非常糟糕的影响,并且使它们不准确。”
忽略这些信息并假设我总是想四舍五入,我该如何在 numpy 中做到这一点?假设我的数组可能非常大。
为简单起见,假设我有数组:
import numpy as np
A = [ [10, 15, 30], [25, 134, 41], [134, 413, 51]]
A = np.array(A, dtype=np.int16)
decimal = A * .1
whole = np.round(decimal)
十进制看起来像:
[[ 1. 1.5 3. ]
[ 2.5 13.4 4.1]
[ 13.4 41.3 5.1]]
整体看起来像:
[[ 1. 2. 3.]
[ 2. 13. 4.]
[ 13. 41. 5.]]
如您所见,1.5 四舍五入为 2,2.5 也四舍五入为 2。如何强制始终获得 XX.5 的四舍五入答案?我知道我可以循环遍历数组并使用 python round() 但这肯定会慢得多。想知道是否有办法使用 numpy 函数来做到这一点
最佳答案
答案几乎永远不会np.vectorize
。您可以而且应该以完全矢量化的方式执行此操作。假设 x >= 0
,你想要r = floor(x + 0.5)
。如果您希望负数向零舍入,则相同的公式适用于 x < 0
。假设您总是想从零舍入。在这种情况下,您正在寻找 ceil(x - 0.5)
对于 x < 0
.
在不调用np.vectorize
的情况下为整个数组实现这一点,您可以使用屏蔽:
def round_half_up(x):
mask = (x >= 0)
out = np.empty_like(x)
out[mask] = np.floor(x[mask] + 0.5)
out[~mask] = np.ceil(x[~mask] - 0.5)
return out
请注意,如果您向一个方向舍入所有内容,则不需要使用 mask :
def round_up(x):
return np.floor(x + 0.5)
现在,如果您想让其真正高效,您可以删除所有临时数组。这将使用 ufunc 的全部功能:
def round_half_up(x):
out = x.copy()
mask = (out >= 0)
np.add(out, 0.5, where=mask, out=out)
np.floor(out, where=mask, out=out)
np.invert(mask, out=mask)
np.subtract(out, 0.5, where=mask, out=out)
np.ceil(out, where=mask, out=out)
return out
还有:
def round_up(x):
out = x + 0.5
np.floor(out, out=out)
return out
关于python - 如何在 numpy 中始终对 XX.5 进行舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53201470/