Handling zero multiplied with NaN

If you have scipy, use scipy.special.xlogy(p_X,p_X). Not only does it solve your problem, as an added benefit it is also a bit faster than p_X*np.log(p_X).


You can use a np.ma.log, which will mask 0s and use the filled method to fill the masked array with 0:

np.ma.log(p_X).filled(0)

For instance:

np.ma.log(range(5)).filled(0)
# array([0.        , 0.        , 0.69314718, 1.09861229, 1.38629436])

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.ma.log(p_X).filled(0))

In your case you can use nansum since adding 0 in sum is the same thing as ignoring a NaN:

ent_X = -1 * np.nansum(p_X * np.log(p_X))

Tags:

Python

Numpy