# entropy of examples from a classification task with 3 classes
from math import log2
from numpy import asarray
# calculate entropy
def entropy(p):
    return -sum(p[i] * log2(p[i]) for i in range(len(p)))

# class 1
p= asarray([1, 0, 0 ]) + 1e-15
print(entropy(p))
# class 2
p= asarray([0, 1, 0 ]) + 1e-15
print(entropy(p))
# class 3
p= asarray([0, 0, 1]) + 1e-15
print(entropy(p))