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")


(0.1, 0.2, 0.7)
[-3.32192809 -2.32192809 -0.51457317]
[-0.33219281 -0.46438562 -0.36020122]
Out[27]:
1.1567796494470395

In [28]:
P = (0.1, 0.2, 0.7)
calc_entropy(P, base="10")


(0.1, 0.2, 0.7)
[-1.         -0.69897    -0.15490196]
[-0.1        -0.139794   -0.10843137]
Out[28]:
0.34822537285722399

In [29]:
P = (0.1, 0.2, 0.7)
calc_entropy(P, base="e")


(0.1, 0.2, 0.7)
[-2.30258509 -1.60943791 -0.35667494]
[-0.23025851 -0.32188758 -0.24967246]
Out[29]:
0.80181855254333734