如何从np直方图计算熵

Vin*_*nci 4 python numpy entropy histogram

我有一个直方图的例子:

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产生数学域错误:(

Mah*_*hdi 8

您可以使用矢量化代码计算熵:

import numpy as np

mu1 = 10
sigma1 = 10

s1 = np.random.normal(mu1, sigma1, 100000)
hist1 = np.histogram(s1, bins=50, range=(-10,10), density=True)
data = hist1[0]
ent = -(data*np.log(np.abs(data))).sum()
# output: 7.1802159512213191
Run Code Online (Sandbox Code Playgroud)

但是如果你想使用for循环,你可以写:

import numpy as np
import math

mu1 = 10
sigma1 = 10

s1 = np.random.normal(mu1, sigma1, 100000)
hist1 = np.histogram(s1, bins=50, range=(-10,10), density=True)
ent = 0
for i in hist1[0]:
    ent -= i * math.log(abs(i))
print (ent)
# output: 7.1802159512213191
Run Code Online (Sandbox Code Playgroud)