我正在尝试估计随机变量(RVs)的熵,其中涉及步骤计算:p_X * log(p_X)。例如,
import numpy as np
X = np.random.rand(100)
binX = np.histogram(X, 10)[0] #create histogram with 10 bins
p_X = binX / np.sum(binX)
ent_X = -1 * np.sum(p_X * np.log(p_X))
Run Code Online (Sandbox Code Playgroud)
有p_X时应为零,在数学上使整个项为零。但是python将p_X * np.log(p_X)as设为NaN,并将整个总和设为NaN。有什么方法可以管理(无需对NaN进行任何显式检查)p_X * np.log(p_X)何时为零时提供p_X零?感谢您的任何见解和纠正,并在此先感谢:)
我试图运行一个看起来像的代码片段,
import numpy as np
import time
def estimate_mutual_info(X, neurons, bins = 5):
xy = np.histogram2d(X, neurons, bins)[0]
x = np.histogram(X, bins)[0]
y = np.histogram(neurons, bins)[0]
ent_x = -1 * np.sum( x / np.sum(x) * np.log( x / np.sum(x)))
ent_y = -1 * np.sum( y / np.sum(y) * np.log( y / np.sum(y)))
ent_xy = -1 * np.sum( xy / np.sum(xy) * np.log( xy / np.sum(xy)))
return (ent_x + ent_y - ent_xy)
tic = time.time()
X = np.random.rand(12000, 1200)
Y = …Run Code Online (Sandbox Code Playgroud)