将 numpy 舍入到最近的步骤
我想知道如何将 numpy 中的数字舍入到上限或下限阈值,该阈值是预定义步长的函数。希望以更清晰的方式说明,如果我有数字 123 且步长等于 50,我需要将 123 舍入到最接近的 150 或 100,在本例中为 100。我得出了下面的函数,它可以完成这项工作但我想知道是否有更好、更简洁的方法来做到这一点。
预先感谢,
保罗
def getRoundedThresholdv1(a, MinClip):
import numpy as np
import math
digits = int(math.log10(MinClip))+1
b = np.round(a, -digits)
if b > a: # rounded-up
c = b - MinClip
UpLow = np.array((b,c))
else: # rounded-down
c = b + MinClip
UpLow = np.array((c,b))
AbsDelta = np.abs(a - UpLow)
return UpLow[AbsDelta.argmin()]
getRoundedThresholdv1(143, 50)
I would like to know how I can round a number in numpy to an upper or lower threshold which is function of predefined step size. Hopefully stated in a clearer way, if I have the number 123 and a step size equal to 50, I need to round 123 to the closest of either 150 or 100, in this case 100. I came out with function below which does the work but I wonder if there is a better, more succint, way to do this.
Thanks in advance,
Paolo
def getRoundedThresholdv1(a, MinClip):
import numpy as np
import math
digits = int(math.log10(MinClip))+1
b = np.round(a, -digits)
if b > a: # rounded-up
c = b - MinClip
UpLow = np.array((b,c))
else: # rounded-down
c = b + MinClip
UpLow = np.array((c,b))
AbsDelta = np.abs(a - UpLow)
return UpLow[AbsDelta.argmin()]
getRoundedThresholdv1(143, 50)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pb360 的解决方案要好得多,使用 python3 中内置 round 的第二个参数。
我认为你不需要
numpy
:这里
a
是一个数字,如果你想向量化这个函数你只需要替换round
与np.round
和float(a)
与np.array(a, dtype=float)
The solution by pb360 is much better, using the second argument of builtin round in python3.
I think you don't need
numpy
:here
a
is a single number, if you want to vectorize this function you only need to replaceround
withnp.round
andfloat(a)
withnp.array(a, dtype=float)
摘要:这是一种正确的方法,最佳答案有不起作用的情况:
我的声誉太低,无法对 Ruggero Turra 的最佳答案发表评论并指出问题。然而,它也有不起作用的情况,例如:
无论使用 numpy 还是标准库回合,都会立即返回 13.200000000000001 。我什至通过对该功能进行压力测试都没有发现这一点。它只是在生产代码中使用它时出现并抛出错误。
请注意,此答案的全部功劳来自开源 github 存储库,该存储库不是我发现的 这里
Summary: This is a correct way to do it, the top answer has cases that do not work:
My reputation is too low to post a comment on the top answer from Ruggero Turra and point out the issue. However it has cases which did not work for example:
Returns 13.200000000000001 right back whether using numpy or the standard library round. I didn't even find this by stress testing the function. It just came up when using it in production code and spat an error.
Note full credit for this answer comes out of an open source github repo which is not mine found here
请注意,在 Ruggero Turra 中,round() 的答案会四舍五入到最接近的偶数。含义:
这可能不是你所期望的。
如果您想要“经典”舍入,可以使用此函数,它支持标量和 Numpy 数组:
或者,您可以使用 Numpy 的方法
digitize
。它要求您定义步骤数组。数字化
会将您的价值ceil
到下一步。因此,为了以“经典”方式进行舍入,我们需要一个中间步骤。您可以使用它:
然后您可以像这样调用它:
这给出:
Note that
round()
in Ruggero Turra his answer rounds to the nearest even integer. Meaning:Which may not be what you expect.
In case you want 'classical' rounding, you can use this function, which supports both scalars and Numpy arrays:
Alternatively, you could use Numpy's method
digitize
. It requires you to define the array of your steps.digitize
will kind ofceil
your value to the next step. So in order to round in a 'classical' way we need an intermediate step.You can use this:
You can then call it like:
Which gives: