RuntimeWarning:除以日志中遇到的零 [英] RuntimeWarning: divide by zero encountered in log
问题描述
我正在使用numpy.log10来计算概率值数组的对数.数组中有一些零,我正在尝试使用
I am using numpy.log10 to calculate the log of an array of probability values. There are some zeros in the array, and I am trying to get around it using
result = numpy.where(prob > 0.0000000001, numpy.log10(prob), -10)
但是,RuntimeWarning: divide by zero encountered in log10
仍然出现,并且我确定这是引起警告的行.
However, RuntimeWarning: divide by zero encountered in log10
still appeared and I am sure it is this line caused the warning.
尽管我的问题已解决,但我很困惑为什么一次又一次出现此警告?
Although my problem is solved, I am confused why this warning appeared again and again?
推荐答案
numpy.log10(prob)
为prob
的所有元素(甚至是where
未选择的元素)计算以10为底的对数.如果需要,可以在使用对数消除问题之前,用10**-10
或一些哑数值填充prob
的零. (不过,请确保您不使用哑数值来计算prob > 0.0000000001
.)
numpy.log10(prob)
calculates the base 10 logarithm for all elements of prob
, even the ones that aren't selected by the where
. If you want, you can fill the zeros of prob
with 10**-10
or some dummy value before taking the logarithm to get rid of the problem. (Make sure you don't compute prob > 0.0000000001
with dummy values, though.)
这篇关于RuntimeWarning:除以日志中遇到的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!