上的数学域错误数学日志

2024-10-01 11:39:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尝试了现有的解决方案,但无法解决这个问题。在

原代码:

 prob = n / self.totaldocs # 0.7445791360764896
 self.classpriorprob[cls] = math.log(prob) 

解决方案:(但我更喜欢使用普通日志)

^{pr2}$

如前所述here:我试图将数字四舍五入到小数点后3位。在

prob = n / self.totaldocs
number = round(prob, 3) # 0.744
self.classpriorprob[cls] = math.log(number)

但我仍然得到数学领域的错误。在

编辑:我直接传递了值,即。数学日志(0.744)并且有效。 当我尝试的时候它也有用数学日志()函数。在

请告知。在

规格
python 3.6.3
皮查姆


Tags: selflognumberhere数字数学math解决方案
2条回答

尝试使用log sum exp技巧:

 def findMaxArray(self,arr):
        maxValue = arr[0]
        for i in range(0, len(arr)):
            if(maxValue <arr[i]):
                maxValue = arr[i]
        return maxValue


    def logExpSum(self, arr ):
        #find maximum of array assuming the array passed is already containg log values
        maxVal =0
        maxVal= self.findMaxArray(arr)
        res = 0
        for i in range(0, len(arr)):
            res += math.exp (arr[i] - maxVal) 
        return (math.log(res)+ maxVal) 

我得到了0.0。在

在我的例子中,解决方案是跳过0.0值。在

if number > 0:
     self.classpriorprob[cls] = math.log(number)

相关问题 更多 >