1.決策樹的構造
優點:計算復雜度不高,輸出結果易於理解,對中間值的缺失不敏感,可以處理不相關特征數據
缺點:可能會產生過度匹配問題
適用數據類型:數值型和標稱型
# coding:utf-8
# !/usr/bin/env python
'''
Created on Oct 12, 2010
Decision Tree Source Code for Machine Learning in Action Ch. 3
@author: Peter Harrington
'''
from math import log
import operator
#通過是否浮出水面和是否有腳蹼,來划分魚類和非魚類
def createDataSet():
dataSet = [[1, 1, 'yes'],
[1, 1, 'yes'],
[1, 0, 'no'],
[0, 1, 'no'],
[0, 1, 'no']]
labels = ['no surfacing','flippers']
#change to discrete values
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) #以2為底求對數
#香農熵Ent的值越小,純度越高,即通過這個特征屬性來分類,屬於同一類別的結點會比較多
return shannonEnt
def splitDataSet(dataSet, axis, value):
retDataSet = []
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis] #chop out axis used for splitting
reducedFeatVec.extend(featVec[axis+1:])
retDataSet.append(reducedFeatVec)
return retDataSet
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0]) - 1 #the last column is used for the labels
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0; bestFeature = -1
for i in range(numFeatures): #iterate over all the features
featList = [example[i] for example in dataSet]#create a list of all the examples of this feature
uniqueVals = set(featList) #get a set of unique values
newEntropy = 0.0
for value in uniqueVals:
subDataSet = splitDataSet(dataSet, i, value)
prob = len(subDataSet)/float(len(dataSet))
newEntropy += prob * calcShannonEnt(subDataSet)
infoGain = baseEntropy - newEntropy #calculate the info gain; ie reduction in entropy
if (infoGain > bestInfoGain): #compare this to the best gain so far
bestInfoGain = infoGain #if better than current best, set to best
bestFeature = i
return bestFeature #returns an integer
def majorityCnt(classList):
classCount={}
for vote in classList:
if vote not in classCount.keys(): classCount[vote] = 0
classCount[vote] += 1
sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
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]#stop splitting when all of the classes are equal
if len(dataSet[0]) == 1: #stop splitting when there are no more features in dataSet
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[:] #copy all of labels, so trees don't mess up existing labels
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value),subLabels)
return myTree
def classify(inputTree,featLabels,testVec):
firstStr = inputTree.keys()[0]
secondDict = inputTree[firstStr]
featIndex = featLabels.index(firstStr)
key = testVec[featIndex]
valueOfFeat = secondDict[key]
if isinstance(valueOfFeat, dict):
classLabel = classify(valueOfFeat, featLabels, testVec)
else: classLabel = valueOfFeat
return classLabel
def storeTree(inputTree,filename):
import pickle
fw = open(filename,'w')
pickle.dump(inputTree,fw)
fw.close()
def grabTree(filename):
import pickle
fr = open(filename)
return pickle.load(fr)
if __name__ == '__main__':
myDat,labels = createDataSet()
print myDat
print calcShannonEnt(myDat)


#通過是否浮出水面和是否有腳蹼,來划分魚類和非魚類
def createDataSet():
dataSet = [[1, 1, 'yes'],
[1, 1, 'yes'],
[1, 0, 'no'],
[0, 1, 'no'],
[0, 1, 'no']]
labels = ['no surfacing','flippers']
#change to discrete values
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) #以2為底求對數
#香農熵Ent的值越小,純度越高,即通過這個特征屬性來分類,屬於同一類別的結點會比較多
return shannonEnt
myDat,labels = createDataSet() print myDat print calcShannonEnt(myDat)

2.划分數據集

def splitDataSet(dataSet, axis, value): #按照給定特征划分數據集,axis表示根據第幾個特征,value表示特征的值
retDataSet = [] #創建新的list對象
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis] #切片
reducedFeatVec.extend(featVec[axis+1:]) #把序列添加到列表reducedFeatVec中
#print reducedFeatVec
retDataSet.append(reducedFeatVec) #把對象reducedFeatVec(是一個list)添加到列表retDataSet中
return retDataSet
def chooseBestFeatureToSplit(dataSet): #選擇最好的數據集划分方式
numFeatures = len(dataSet[0]) - 1 #特征的數量,最后一列是標簽,所以減去1
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0; bestFeature = -1 #信息增益和最好的特征下標
for i in range(numFeatures): #遞歸所有特征
featList = [example[i] for example in dataSet] #創建一個列表,包含第i個特征的所有值
uniqueVals = set(featList) #創建一個集合set,由不同的元素組成
newEntropy = 0.0
for value in uniqueVals:
subDataSet = splitDataSet(dataSet, i, value) #按照所有特征的可能划分數據集
prob = len(subDataSet)/float(len(dataSet)) #計算所有特征的可能性
newEntropy += prob * calcShannonEnt(subDataSet)
infoGain = baseEntropy - newEntropy #計算信息增益
if (infoGain > bestInfoGain): #比較不同特征之間信息增益的大小
bestInfoGain = infoGain #選取信息增益大的特征
bestFeature = i
return bestFeature #返回特征的下標

3.遞歸構建決策樹

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) #遞歸createTree
return myTree
myDat,labels = createDataSet()
myTree = createTree(myDat,labels)
print myTree
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}

4.在Python中使用Matplotlib注解繪制樹形圖
myDat,labels = createDataSet() print myDat import treePlotter treePlotter.createPlot(myTree) #繪制樹形圖

5.構造注解樹
獲取葉節點的數目和樹的層數
import matplotlib.pyplot as plt
decisionNode = dict(boxstyle="sawtooth", fc="0.8")
leafNode = dict(boxstyle="round4", fc="0.8")
arrow_args = dict(arrowstyle="<-")
def getNumLeafs(myTree): #獲取葉子節點的數目
numLeafs = 0
firstStr = myTree.keys()[0]
secondDict = myTree[firstStr]
for key in secondDict.keys():
if type(secondDict[key]).__name__=='dict': #測試節點的數據類型是否為字典
numLeafs += getNumLeafs(secondDict[key]) #遞歸
else: numLeafs +=1 #如果不是字典,則說明是葉子節點
return numLeafs
def getTreeDepth(myTree): #獲取樹的層數
maxDepth = 0
firstStr = myTree.keys()[0]
secondDict = myTree[firstStr]
for key in secondDict.keys():
if type(secondDict[key]).__name__=='dict': #測試節點的數據類型是否為字典,如果不是字典,則說明是葉子節點
thisDepth = 1 + getTreeDepth(secondDict[key]) #遞歸
else: thisDepth = 1
if thisDepth > maxDepth: maxDepth = thisDepth
return maxDepth
繪制樹形圖


def plotNode(nodeTxt, centerPt, parentPt, nodeType): #繪制帶箭頭的注解
#annotate參數:nodeTxt:標注文本,xy:所要標注的位置坐標,xytext:標注文本所在位置,arrowprops:標注箭頭屬性信息
createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction',
xytext=centerPt, textcoords='axes fraction',
va="center", ha="center", bbox=nodeType, arrowprops=arrow_args )
def plotMidText(cntrPt, parentPt, txtString): #在父子節點間填充文本信息
xMid = (parentPt[0]-cntrPt[0])/2.0 + cntrPt[0]
yMid = (parentPt[1]-cntrPt[1])/2.0 + cntrPt[1]
createPlot.ax1.text(xMid, yMid, txtString, va="center", ha="center", rotation=30)
def plotTree(myTree, parentPt, nodeTxt): #if the first key tells you what feat was split on
numLeafs = getNumLeafs(myTree) #計算寬與高
depth = getTreeDepth(myTree)
firstStr = myTree.keys()[0] #the text label for this node should be this
print plotTree.xOff
cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW, plotTree.yOff)
print parentPt
print cntrPt
plotMidText(cntrPt, parentPt, nodeTxt) #標記子節點屬性值
plotNode(firstStr, cntrPt, parentPt, decisionNode)
secondDict = myTree[firstStr]
plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD #減少y偏移
for key in secondDict.keys():
if type(secondDict[key]).__name__=='dict': #test to see if the nodes are dictonaires, if not they are leaf nodes
plotTree(secondDict[key],cntrPt,str(key)) #recursion
else: #it's a leaf node print the leaf node
plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW
plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD
#if you do get a dictonary you know it's a tree, and the first element will be another dict
def createPlot(inTree): #繪制樹形圖,調用了plotTree()
fig = plt.figure(1, facecolor='white')
fig.clf()
axprops = dict(xticks=[], yticks=[])
createPlot.ax1 = plt.subplot(111, frameon=False, **axprops) #no ticks
#createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
plotTree.totalW = float(getNumLeafs(inTree)) #存儲樹的寬度
plotTree.totalD = float(getTreeDepth(inTree)) #存儲樹的深度
plotTree.xOff = -0.5/plotTree.totalW; plotTree.yOff = 1.0;
plotTree(inTree, (0.5,1.0), '')
plt.show()
測試和存儲分類器
1.測試算法:使用決策樹執行分類
def classify(inputTree,featLabels,testVec): #使用決策樹的分類函數
firstStr = inputTree.keys()[0]
secondDict = inputTree[firstStr]
featIndex = featLabels.index(firstStr) #將標簽字符串轉換為索引
key = testVec[featIndex]
valueOfFeat = secondDict[key]
if isinstance(valueOfFeat, dict):
classLabel = classify(valueOfFeat, featLabels, testVec)
else: classLabel = valueOfFeat
return classLabel
myDat,labels = createDataSet()
Labels = labels
print "myDat="
print myDat
print "labels="
print labels
import treePlotter
myTree = treePlotter.retrieveTree(0) #繪制樹形圖
print myTree
print classify(myTree,Labels,[0,1])
2.使用算法:決策樹的存儲

def storeTree(inputTree,filename): #使用pickle模塊存儲決策樹
import pickle
fw = open(filename,'w')
pickle.dump(inputTree,fw)
fw.close()
def grabTree(filename): #查看決策樹
import pickle
fr = open(filename)
return pickle.load(fr)
myDat,labels = createDataSet()
Labels = labels
print "myDat="
print myDat
print "labels="
print labels
import treePlotter
myTree = treePlotter.retrieveTree(0) #繪制樹形圖
print myTree
storeTree(myTree,'classifierStorage.txt')
print grabTree('classifierStorage.txt')
示例:使用決策樹預測隱形眼鏡類型


import treePlotter
import simplejson
import ch
ch.set_ch()
from matplotlib import pyplot as plt
fr = open('lenses.txt')
lenses = [inst.strip().split('\t') for inst in fr.readlines()] #讀取一行數據,以tab鍵分割並去掉空格
lensesLabels = [u'年齡',u'近遠視',u'散光',u'眼淚等級'] #使用unicode,不然編碼會報錯
lensesTree = createTree(lenses,lensesLabels)
print simplejson.dumps(lensesTree, encoding="UTF-8", ensure_ascii=False) #使用simplejson模塊輸出對象中的中文
treePlotter.createPlot(lensesTree)


