【发布时间】:2013-05-14 08:43:48
【问题描述】:
我正在阅读Machine Learning In Action 并且正在阅读决策树章节。我知道决策树的构建使得拆分数据集为您提供了一种构建分支和叶子的方法。这会在树的顶部为您提供更有可能的信息,并限制您需要做出多少决定。
这本书展示了一个确定数据集香农熵的函数:
def calcShannonEnt(dataSet):
numEntries = len(dataSet)
labelCounts = {}
for featVec in dataSet: #the the number of unique elements and their occurance
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) #log base 2
return shannonEnt
其中输入数据集是一个数组,其中每个数组代表一个潜在的可分类特征:
dataSet = [[1, 1, 'yes'],
[1, 1, 'yes'],
[1, 0, 'no'],
[0, 1, 'no'],
[0, 1, 'no']]
我不明白为什么本书中的香农熵函数只查看特征数组中的最后一个元素?看起来它只计算“是”或“否”项目的熵,而不是任何其他特征的熵?
这对我来说没有意义,因为这个数据集的熵
dataSet = [[1, 1, 'yes'],
[1, 'asdfasdf', 'yes'],
[1900, 0, 'no'],
[0, 1, 'no'],
['ddd', 1, 'no']]
与上面的熵相同,尽管它有更多不同的数据。
为了给出数据集的总熵,是不是应该把其他特征元素也算在内,还是我误解了熵计算应该做什么?
如果有人好奇,本书的完整源代码(即代码的来源)是 Chapter03 文件夹下的 here。
【问题讨论】:
标签: python machine-learning decision-tree entropy