+/IBCS

git clone https://git.julia.blue/+/IBCS

Joseph Ryanadd final projectc6bf06e

master
4.3 KiB143 linesraw
1'''
2Created on Oct 14, 2010
3
4@author: Peter Harrington
5'''
6import matplotlib.pyplot as plt
7
8decisionNode = dict(boxstyle="sawtooth", fc="0.8")
9leafNode = dict(boxstyle="round4", fc="0.8")
10arrow_args = dict(arrowstyle="<-")
11
12
13def getNumLeafs(myTree):
14    numLeafs = 0
15    firstStr = myTree.keys()[0]
16    secondDict = myTree[firstStr]
17    for key in secondDict.keys():
18        if type(
19                secondDict[key]
20        ).__name__ == 'dict':  #test to see if the nodes are dictonaires, if not they are leaf nodes
21            numLeafs += getNumLeafs(secondDict[key])
22        else:
23            numLeafs += 1
24    return numLeafs
25
26
27def getTreeDepth(myTree):
28    maxDepth = 0
29    firstStr = myTree.keys()[0]
30    secondDict = myTree[firstStr]
31    for key in secondDict.keys():
32        if type(
33                secondDict[key]
34        ).__name__ == 'dict':  #test to see if the nodes are dictonaires, if not they are leaf nodes
35            thisDepth = 1 + getTreeDepth(secondDict[key])
36        else:
37            thisDepth = 1
38        if thisDepth > maxDepth:
39            maxDepth = thisDepth
40    return maxDepth
41
42
43def plotNode(nodeTxt, centerPt, parentPt, nodeType):
44    createPlot.ax1.annotate(
45        nodeTxt,
46        xy=parentPt,
47        xycoords='axes fraction',
48        xytext=centerPt,
49        textcoords='axes fraction',
50        va="center",
51        ha="center",
52        bbox=nodeType,
53        arrowprops=arrow_args)
54
55
56def plotMidText(cntrPt, parentPt, txtString):
57    xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0]
58    yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]
59    createPlot.ax1.text(
60        xMid, yMid, txtString, va="center", ha="center", rotation=30)
61
62
63def plotTree(myTree, parentPt,
64             nodeTxt):  #if the first key tells you what feat was split on
65    numLeafs = getNumLeafs(myTree)  #this determines the x width of this tree
66    # depth = getTreeDepth(myTree)
67    firstStr = myTree.keys()[0]  #the text label for this node should be this
68    cntrPt = (plotTree.xOff + (1.0 + float(numLeafs)) / 2.0 / plotTree.totalW,
69              plotTree.yOff)
70    plotMidText(cntrPt, parentPt, nodeTxt)
71    plotNode(firstStr, cntrPt, parentPt, decisionNode)
72    secondDict = myTree[firstStr]
73    plotTree.yOff = plotTree.yOff - 1.0 / plotTree.totalD
74    for key in secondDict.keys():
75        if type(
76                secondDict[key]
77        ).__name__ == 'dict':  #test to see if the nodes are dictonaires, if not they are leaf nodes
78            plotTree(secondDict[key], cntrPt, str(key))  #recursion
79        else:  #it's a leaf node print the leaf node
80            plotTree.xOff = plotTree.xOff + 1.0 / plotTree.totalW
81            plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt,
82                     leafNode)
83            plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
84    plotTree.yOff = plotTree.yOff + 1.0 / plotTree.totalD
85
86
87# if you do get a dictonary you know it's a tree, and the first element will be another dict
88
89
90def createPlot(inTree):
91    fig = plt.figure(1, facecolor='white')
92    fig.clf()
93    axprops = dict(xticks=[], yticks=[])
94    createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)  #no ticks
95    # createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
96    plotTree.totalW = float(getNumLeafs(inTree))
97    plotTree.totalD = float(getTreeDepth(inTree))
98    plotTree.xOff = -0.5 / plotTree.totalW
99    plotTree.yOff = 1.0
100    plotTree(inTree, (0.5, 1.0), '')
101    plt.show()
102
103
104# def createPlot():
105#    fig = plt.figure(1, facecolor='white')
106#    fig.clf()
107#    createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
108#    plotNode('a decision node', (0.5, 0.1), (0.1, 0.5), decisionNode)
109#    plotNode('a leaf node', (0.8, 0.1), (0.3, 0.8), leafNode)
110#    plt.show()
111
112
113def retrieveTree(i):
114    listOfTrees = [{
115        'no surfacing': {
116            0: 'no',
117            1: {
118                'flippers': {
119                    0: 'no',
120                    1: 'yes'
121                }
122            }
123        }
124    }, {
125        'no surfacing': {
126            0: 'no',
127            1: {
128                'flippers': {
129                    0: {
130                        'head': {
131                            0: 'no',
132                            1: 'yes'
133                        }
134                    },
135                    1: 'no'
136                }
137            }
138        }
139    }]
140    return listOfTrees[i]
141
142
143# createPlot(thisTree)