#trees.py
#计算给定数据集的香农熵
from math import log

def createDataSet():#创建数据集和标签集
    dataSet = [[1,1,'maybe'],[1,0,'yes'],[1,1,'yes'],[0,1,'no'],[0,0,'no']]
    labels = ['no surfacing','flippers']
    return dataSet,labels

def calcShannonEnt(dataSet):#计算香农熵
    numEntries = len(dataSet)
    labelCounts = {}
    for featVec in dataSet:
        currentLabel = featVec[-1] #读取当前实例类别
        if currentLabel not in labelCounts.keys(): #将类别加入到字典中，并统计类别数目
            labelCounts[currentLabel] = 0
        labelCounts[currentLabel] +=1
    shannonEnt = 0.0
    for key in labelCounts: #计算概率和熵
        prob = float(labelCounts[key])/numEntries
        shannonEnt -=prob*log(prob,2)
    return shannonEnt

#test.py
#测试，计算香农熵
import trees
from math import log
myDat,labels = trees.createDataSet()
print(myDat)
shannonEnt=trees.calcShannonEnt(myDat)
print(shannonEnt)
