我应该如何处理那些可以变得很小以至于变成零的浮动数字 [英] How should I deal with floating numbers that numbers that can get so small that the become zero
问题描述
<$ c
b = b
$ b $(如果(wp > 0):
p * = wp
#取自然对数,当'probabilites'足够长,p结束
#为零
尝试:
result = math.log(p)
因为结果不需要确切地说,我解决了这个问题,只需保持最小的非零值,并使用它,如果p永远变成0。
如果(wp> 0):
$ b old_p = p
p * = wp
如果p == 0:
#我们已经变得如此小,它只是0,所以回到最小
#非零,我们有
p = old_p
bre ak
#以自然记录为例,当'probabilites'足够长以至于p结束时
#为零
try:
result = math.log (p)
这个方法可行,但对我来说似乎有点不方便。我不会做这么多的数字编程,我不确定这是否是人们使用的修复方法,或者是有更好的方法可以去做。因为
math.log(a)+ math.log(b)
,为什么不把所有这样可以避免 p
变小的问题。
编辑:这是numpy版本,对于大型数据集来说它更干净,速度更快:
import numpy
prob = numpy.array([0.1,0.213,0.001,0.98 ...])
result = sum(numpy.log(prob))
So I just fixed an interesting bug in the following code, but I'm not sure the approach I took it the best:
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)
Because the result doesn't need to be exact, I solved this by simply keeping the smallest non-zero value, and using that if p ever becomes 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)
This works, but it seems a bit kludgy to me. I don't do a ton of this kind of numerical programming, and I'm not sure if this is the kind of fix people use, or if there is something better I can go for.
Since, math.log(a * b)
is equal to math.log(a) + math.log(b)
, why not take a sum of the logs of all members of the probabilities
array?
This will avoid the problem of p
getting so small it under-flows.
Edit: this is the numpy version, which is cleaner and a lot faster for large data sets:
import numpy
prob = numpy.array([0.1, 0.213, 0.001, 0.98 ... ])
result = sum(numpy.log(prob))
这篇关于我应该如何处理那些可以变得很小以至于变成零的浮动数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!