<filename>Entropy/4.py<gh_stars>0
# 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))
<filename>Entropy/4.py<gh_stars>0
# 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))
# entropy of examples from a classification task with 3 classes # calculate entropy # class 1 # class 2 # class 3