MCPcopy Create free account
hub / github.com/Jack-Cherish/Machine-Learning / plotTree

Function plotTree

Decision Tree/Decision Tree.py:297–315  ·  view source on GitHub ↗
(myTree, parentPt, nodeTxt)

Source from the content-addressed store, hash-verified

295 2017-07-24
296"""
297def plotTree(myTree, parentPt, nodeTxt):
298 decisionNode = dict(boxstyle="sawtooth", fc="0.8") #设置结点格式
299 leafNode = dict(boxstyle="round4", fc="0.8") #设置叶结点格式
300 numLeafs = getNumLeafs(myTree) #获取决策树叶结点数目,决定了树的宽度
301 depth = getTreeDepth(myTree) #获取决策树层数
302 firstStr = next(iter(myTree)) #下个字典
303 cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW, plotTree.yOff) #中心位置
304 plotMidText(cntrPt, parentPt, nodeTxt) #标注有向边属性值
305 plotNode(firstStr, cntrPt, parentPt, decisionNode) #绘制结点
306 secondDict = myTree[firstStr] #下一个字典,也就是继续绘制子结点
307 plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD #y偏移
308 for key in secondDict.keys():
309 if type(secondDict[key]).__name__=='dict': #测试该结点是否为字典,如果不是字典,代表此结点为叶子结点
310 plotTree(secondDict[key],cntrPt,str(key)) #不是叶结点,递归调用继续绘制
311 else: #如果是叶结点,绘制叶结点,并标注有向边属性值
312 plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW
313 plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
314 plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
315 plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD
316
317"""
318函数说明:创建绘制面板

Callers 1

createPlotFunction · 0.85

Calls 4

getNumLeafsFunction · 0.85
getTreeDepthFunction · 0.85
plotMidTextFunction · 0.85
plotNodeFunction · 0.85

Tested by

no test coverage detected