from collections import Counter
from math import log
def calcShannonEnt(dataset):
classCnt = [sample[-1] for sample in dataset]
n = len(dataset)
classCnt = Counter(classCnt)
ent = 0.
for times in classCnt.values():
ent -= times/n*log(times/n, 2)
return ent