我们也可以把决策树绘制出来:
def Plot_node(nodeTxt, centerPt, parentPt, nodeType):
Create_plot.ax1.annotate(nodeTxt, xy=parentPt,
xycoords=‘axes fraction’,
xytext=centerPt, textcoords=‘axes fraction’,
va=“center”, ha=“center”, bbox=nodeType, arrowprops=arrow_args)
def Plot_tree(myTree, parentPt, nodeTxt):
numLeafs = Get_numleafs(myTree)
Get_treedepth(myTree)
firstStr = myTree.keys()[0]
cntrPt = (Plot_tree.xOff + (1.0 + float(numLeafs))/2.0/Plot_tree.totalW,
Plot_tree.yOff)
Plot_midtext(cntrPt, parentPt, nodeTxt)
Plot_node(firstStr, cntrPt, parentPt, decisionNode)
secondDict = myTree[firstStr]
Plot_tree.yOff = Plot_tree.yOff - 1.0/Plot_tree.totalD
for key in secondDict.keys():
if type(secondDict[key]).__name__==‘dict’:
Plot_tree(secondDict[key],cntrPt,str(key))
else:
Plot_tree.xOff = Plot_tree.xOff + 1.0/Plot_tree.totalW
Plot_node(secondDict[key], (Plot_tree.xOff, Plot_tree.yOff),
cntrPt, leafNode)
Plot_midtext((Plot_tree.xOff, Plot_tree.yOff), cntrPt, str(key))
Plot_tree.yOff = Plot_tree.yOff + 1.0/Plot_tree.totalD
def Create_plot (myTree):
fig = plt.figure(1, facecolor = ‘white’)
fig.clf()
axprops = dict(xticks=[], yticks=[])
Create_plot.ax1 = plt.subplot(111, frameon=False, **axprops)
Plot_tree.totalW = float(Get_numleafs(myTree))
Plot_tree.totalD = float(Get_treedepth(myTree))
Plot_tree.xOff = -0.5/Plot_tree.totalW; Plot_tree.yOff = 1.0;
Plot_tree(myTree, (0.5,1.0), ‘’)
plt.show()
def Plot_midtext(cntrPt, parentPt, txtString):
xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0]
yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]
Create_plot.ax1.text(xMid, yMid, txtString)
def Classify(myTree, featLabels, testVec):
firstStr = myTree.keys()[0]
secondDict = myTree[firstStr]
featIndex = featLabels.index(firstStr)
for key in secondDict.keys():
if testVec[featIndex] == key:
if type(secondDict[key]).__name__ == ‘dict’ :
classLabel = Classify(secondDict[key],featLabels,testVec)
else:
classLabel = secondDict[key]
return classLabel
最后,可以测试我们的构造的决策树分类器:
decisionNode = dict(boxstyle=“sawtooth”, fc=“0.8”)
leafNode = dict(boxstyle=“round4”, fc=“0.8”)
arrow_args = dict(arrowstyle=“《-”)
myData, featName = Create_data()
S_entrpy = Cal_entrpy(myData)
new_data = Split_dataset(myData, 0, 1)
best_feat = Choose_feature(myData)
myTree = Create_tree(myData, featName[:])
num_leafs = Get_numleafs(myTree)
depth = Get_treedepth(myTree)
Create_plot(myTree)
predict_label = Classify(myTree, featName, [1, 0])
print(“the predict label is: ”, predict_label)
print(“the decision tree is: ”, myTree)
print(“the best feature index is: ”, best_feat)
print(“the new dataset: ”, new_data)
print(“the original dataset: ”, myData)
print(“the feature names are: ”, featName)
print(“the entrpy is:”, S_entrpy)
print(“the number of leafs is: ”, num_leafs)
print(“the dpeth is: ”, depth)
print(“All is well.”)
构造的决策树最后如下所示:
评论
查看更多