In [24]:
import numpy as np
def calc_entropy (P, base):
logP = None
if base == "2":
logP = np.log2(P)
elif base == "10":
logP = np.log10(P)
elif base == "e":
logP = np.log(P)
print P
print logP
print P*logP
return -sum (P*logP)
In [27]:
P = (0.1, 0.2, 0.7)
calc_entropy(P, base="2")
Out[27]:
In [28]:
P = (0.1, 0.2, 0.7)
calc_entropy(P, base="10")
Out[28]:
In [29]:
P = (0.1, 0.2, 0.7)
calc_entropy(P, base="e")
Out[29]: