我应该如何处理浮点数,这些数字可能变得很小以至于变成零

2024-01-05

所以我刚刚修复了以下代码中的一个有趣的错误,但我不确定我采取的最佳方法:

p = 1
probabilities = [ ... ] # a (possibly) long list of numbers between 0 and 1
for wp in probabilities:

  if (wp > 0):
    p *= wp

# Take the natural log, this crashes when 'probabilites' is long enough that p ends up
# being zero
try:
    result = math.log(p)

因为结果不需要精确,所以我通过简单地保留最小的非零值并在 p 变为 0 时使用它来解决这个问题。

p = 1
probabilities = [ ... ] # a long list of numbers between 0 and 1
for wp in probabilities:

  if (wp > 0):
    old_p = p
    p *= wp
    if p == 0:
      # we've gotten so small, its just 0, so go back to the smallest
      # non-zero we had
      p = old_p
      break

# Take the natural log, this crashes when 'probabilites' is long enough that p ends up
# being zero
try:
    result = math.log(p)

这可行,但对我来说似乎有点笨拙。我不会进行大量此类数值编程,并且我不确定这是否是人们使用的修复方法,或者是否有更好的东西我可以寻求。


Since, math.log(a * b)等于math.log(a) + math.log(b),为什么不将所有成员的日志相加probabilities array?

这将避免以下问题p变得如此之小以致于发生下溢。

编辑:这是 numpy 版本,对于大型数据集来说更干净且速度更快:

import numpy
prob = numpy.array([0.1, 0.213, 0.001, 0.98 ... ])
result = sum(numpy.log(prob))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我应该如何处理浮点数,这些数字可能变得很小以至于变成零 的相关文章

随机推荐