相关疑难解决方法(0)

math.log函数中的python数学域错误

我不得不使用Python math.log(x)函数的值X从(0,...,1).有时x可能太接近于零,Python给我一个错误:

ValueError:数学域错误

我怎么知道,math.log函数定义的领域是什么?

python math

19
推荐指数
2
解决办法
5万
查看次数

python数学域错误-sqrt

是什么原因引起的问题?

from math import sqrt
print "a : "
a = float(raw_input())
print "b : "
b = float(raw_input())
print "c : "
c = float(raw_input())
d = (a + b + c)/2
s = sqrt(d*(d-a)*(d-b)*(d-c))
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
   File "C:/Python27/fájlok/háromszög terület2.py", line 11, in <module>
       s = sqrt(d*(d-a)*(d-b)*(d-c))
ValueError: math domain error
Run Code Online (Sandbox Code Playgroud)

python math dns sqrt

4
推荐指数
2
解决办法
3万
查看次数

如何从np直方图计算熵

我有一个直方图的例子:

mu1 = 10, sigma1 = 10
s1 = np.random.normal(mu1, sigma1, 100000)
Run Code Online (Sandbox Code Playgroud)

并计算

hist1 = np.histogram(s1, bins=50, range=(-10,10), density=True)
for i in hist1[0]:
    ent = -sum(i * log(abs(i)))
print (ent)
Run Code Online (Sandbox Code Playgroud)

现在我想从给定的直方图数组中找到熵,但由于np.histogram返回两个数组,我在计算熵方面遇到了麻烦.我怎样才能调用第一个np.histogram数组并计算熵?即使上面的代码是正确的,我也会得到熵的数学域错误.:(

**编辑:当Mu = 0时如何找到熵?和log(0)产生数学域错误?


所以我想写的实际代码是:

mu1, sigma1 = 0, 1
mu2, sigma2 = 10, 1
s1 = np.random.normal(mu1, sigma1, 100000)
s2 = np.random.normal(mu2, sigma2, 100000)

hist1 = np.histogram(s1, bins=100, range=(-20,20), density=True)
data1 = hist1[0]
ent1 = -(data1*np.log(np.abs(data1))).sum() 

hist2 = np.histogram(s2, bins=100, range=(-20,20), density=True)
data2 = hist2[0]
ent2 = -(data2*np.log(np.abs(data2))).sum() 
Run Code Online (Sandbox Code Playgroud)

到目前为止,第一个示例ent1将产生nan,而第二个示例ent2产生数学域错误:(

python numpy entropy histogram

4
推荐指数
1
解决办法
3133
查看次数

使用math.acos函数的Python数学域错误

我正在使用math.acos()函数:

math.acos(1.0000000000000002)
Run Code Online (Sandbox Code Playgroud)

这会引发数学域错误.有人能说出原因吗?我得到之前计算的这个值,这里这个值给出错误但是如果我在结尾处删除2它不会抛出错误.我没理由这个.

python

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×4

math ×2

dns ×1

entropy ×1

histogram ×1

numpy ×1

sqrt ×1