赞
踩
from math import log 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 # 书中有错 # 0:{"yes":1} 1:{"yes":2} 2:{"no":1} 3:{"no":2} 4:{"no":3} shannonEnt = 0.0 for key in labelCounts: prob = float(labelCounts[key]) / numEntries # 计算概率 # 以2为底求对数 shannonEnt -= prob * log(prob,2) # 递减求和得熵 return shannonEnt # 手动计算: Ent = -0.4*log(2,0.4)-0.6*log(2,0.6) # Ent_mannual = -(0.4 * log(0.4,2)) - (0.6 * log(0.6,2)) # print(Ent_mannual) # 写一个数据集 def createDataSet(): dataSet = [['<=30', 'high', 'no', 'fair', 'no'], ['<=30', 'high', '
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。