Decison Tree的注释:画图部分不给注释了
from math import log
import numpy
def calcShannonEnt(dataSet):
numEntries = len(dataSet)
labelCounts = {}
#这个是字典,{a:1,b:2}其中a,b是key,1,2是对应的value
for featVec in dataSet:
currentLabel = featVec[-1]
#-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
def createDataSet():
dataSet=[[1,1,'yes'],
[1,1,'yes'],
[1,0,'no'],
[0,1,'yes'],
[0,1,'no']]
labels=['no surfacing','flippers']
return dataSet,labels
#依据特征划分数据集 axis代表第几个特征 value代表该特征所对应的值 返回的是划分后的数据集
def splitDataSet(dataSet, axis, value):
retDataSet = []
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis]
#这里的featVec[:axis],是指从第1(就是下标0)个数到第axis个,不包含
reducedFeatVec.extend(featVec[axis+1:])
#同上,这里的[axis+1,:]就是从最后到axis+1
retDataSet.append(reducedFeatVec)
#extend,append都是扩展用的,a=[1,2],b=[3,4],a.append(b)=[1,2,[3,4]],a.extend(b)=[1,2,3,4]
return retDataSet
#选择最好的数据集(特征)划分方式 返回最佳特征下标
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0]) - 1 #特征个数
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0; bestFeature = -1
for i in range(numFeatures): #遍历特征 第i个
featureSet = set([example[i] for example in dataSet]) #第i个特征取值集合
#这一部分代码没啥难度,跟matalb差不多,唯一就是这个set
newEntropy= 0.0
for value in featureSet:
subDataSet = splitDataSet(dataSet, i, value)
prob = len(subDataSet)/float(len(dataSet))
newEntropy += prob * calcShannonEnt(subDataSet) #该特征划分所对应的entropy
infoGain = baseEntropy - newEntropy
if infoGain > bestInfoGain:
bestInfoGain = infoGain
bestFeature = i
return bestFeature
#创建树的函数代码 python中用字典类型来存储树的结构 返回的结果是myTree-字典
def createTree(dataSet, labels):
classList = [example[-1] for example in dataSet]
if classList.count(classList[0]) == len(classList): #类别完全相同则停止继续划分 返回类标签-叶子节点
return classList[0]
if len(dataSet[0]) == 1:
return majorityCnt(classList) #遍历完所有的特征时返回出现次数最多的
bestFeat = chooseBestFeatureToSplit(dataSet)
bestFeatLabel = labels[bestFeat]
myTree = {bestFeatLabel:{}}
del(labels[bestFeat])
featValues = [example[bestFeat] for example in dataSet] #得到的列表包含所有的属性值
uniqueVals = set(featValues)
for value in uniqueVals:
subLabels = labels[:]
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels)
return myTree
#多数表决的方法决定叶子节点的分类 ---- 当所有的特征全部用完时仍属于多类
def majorityCnt(classList):
classCount = {}
for vote in classList:
if vote not in classCount.key():
classCount[vote] = 0;
classCount[vote] += 1
sortedClassCount = sorted(classCount.iteritems(), key = operator.itemgetter(1), reverse = True)
#排序函数,至于怎么用,help就好,里面参数设置有详细例子
return sortedClassCount[0][0]
创建树的函数代码 其实这一步应该放在上一步前面
def createTree(dataSet, labels):
classList = [example[-1] for example in dataSet]
if classList.count(classList[0]) == len(classList): #类别完全相同则停止继续划分 返回类标签-叶子节点
return classList[0]
#count是数数目的函数,a=[1,1,2] a.count[1]=2 len相当于matalb里的length
if len(dataSet[0]) == 1:
return majorityCnt(classList) #遍历完所有的特征时返回出现次数最多的
bestFeat = chooseBestFeatureToSplit(dataSet)
bestFeatLabel = labels[bestFeat]
myTree = {bestFeatLabel:{}}
del(labels[bestFeat])
featValues = [example[bestFeat] for example in dataSet] #得到的列表包含所有的属性值
uniqueVals = set(featValues)
for value in uniqueVals:
subLabels = labels[:]
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels)
#这一步creteTree里面又用了creatTree,递归调用,直到len(dataSet[0]) == 1:
return myTree