MCPcopy Index your code
hub / github.com/wepe/MachineLearning / plotTree

Function plotTree

DecisionTree/treePlotter.py:45–61  ·  view source on GitHub ↗
(myTree, parentPt, nodeTxt)

Source from the content-addressed store, hash-verified

43 createPlot.ax1.text(xMid, yMid, txtString, va="center", ha="center", rotation=30)
44
45def plotTree(myTree, parentPt, nodeTxt):#if the first key tells you what feat was split on
46 numLeafs = getNumLeafs(myTree) #this determines the x width of this tree
47 #depth = getTreeDepth(myTree)
48 firstStr = myTree.keys()[0] #the text label for this node should be this
49 cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW, plotTree.yOff)
50 plotMidText(cntrPt, parentPt, nodeTxt)
51 plotNode(firstStr, cntrPt, parentPt, decisionNode)
52 secondDict = myTree[firstStr]
53 plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD
54 for key in secondDict.keys():
55 if type(secondDict[key]).__name__=='dict':#test to see if the nodes are dictonaires, if not they are leaf nodes
56 plotTree(secondDict[key],cntrPt,str(key)) #recursion
57 else: #it's a leaf node print the leaf node
58 plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW
59 plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
60 plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
61 plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD
62#if you do get a dictonary you know it's a tree, and the first element will be another dict
63
64def createPlot(inTree):

Callers 1

createPlotFunction · 0.85

Calls 3

getNumLeafsFunction · 0.85
plotMidTextFunction · 0.85
plotNodeFunction · 0.85

Tested by

no test coverage detected