gpt4 book ai didi

python - numpy.round 的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 21:41:10 25 4
gpt4 key购买 nike

Python 的 round() 似乎总是在遇到 x.5 数字时四舍五入:

print round(1.5),round(2.5),round(3.5),round(4.5)
>>> 2.0 3.0 4.0 5.0

但是numpy.round()似乎不一致:

import numpy as np
print np.round(1.5),np.round(2.5),np.round(3.5),np.round(4.5)
>>> 2.0 2.0 4.0 4.0

这在某些情况下可能会引入错误。这是错误还是我遗漏了什么?

最佳答案

numpy 舍入到最接近的偶数:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.around.html#numpy.around

For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc.

关于python - numpy.round 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45021268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com