Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ treeExplorer.py for Machine Learning in Action, Chapter 9.
- I was getting errors galore using _tkagg so I just split
- the gui into two to get a working example.
- --lolamontes69
- """
- from numpy import *
- from Tkinter import *
- import regTrees
- import matplotlib.pyplot as plt
- def reDraw(tolS,tolN):
- f = plt.figure('Tree Explorer - sine.txt')
- f.clf()
- a = f.add_subplot(111)
- if chkBtnVar.get():
- if tolN < 2: tolN = 2
- myTree=regTrees.createTree(reDraw.rawDat, regTrees.modelLeaf,\
- regTrees.modelErr, (tolS,tolN))
- yHat = regTrees.createForeCast(myTree, reDraw.testDat,regTrees.modelTreeEval)
- else:
- myTree=regTrees.createTree(reDraw.rawDat, ops=(tolS,tolN))
- yHat = regTrees.createForeCast(myTree, reDraw.testDat)
- a.scatter(reDraw.xcord1, reDraw.ycord1, s=5)
- a.plot(reDraw.testDat, yHat, linewidth=2.0)
- plt.show()
- def getInputs():
- try: tolN = int(tolNentry.get())
- except:
- tolN = 10
- print "enter Integer for tolN"
- tolNentry.delete(0, END)
- tolNentry.insert(0,'10')
- try: tolS = float(tolSentry.get())
- except:
- tolS = 1.0
- print "enter Float for tolS"
- tolSentry.delete(0, END)
- tolSentry.insert(0,'1.0')
- return tolN,tolS
- def drawNewTree():
- tolN,tolS = getInputs()
- reDraw(tolS,tolN)
- root=Tk()
- root.title("Tree Explorer - sine.txt")
- text1 = "\nUSING TREE EXPLORER.\n\n\
- tolN and tolS are used for pre-pruning the tree. \n\
- tolN - minimum number of data instances to include in tree splits \n\
- tolS - a tolerance on the error reduction \n\
- \n\
- To draw the first plot; \n\
- 1: press the <Draw/Redraw> button \n\
- \n\
- To try new tolN and tolS values; \n\
- 1: close the plot \n\
- 2: enter the new values in the boxes below \n\
- 3: press <Draw/Redraw> \n\n"
- Label(root, text=text1).grid(row=0, column=0, columnspan=4)
- Label(root, text="tolN").grid(row=2, column=0)
- tolNentry = Entry(root)
- tolNentry.grid(row=2, column=1)
- tolNentry.insert(0,'10')
- Label(root, text="tolS").grid(row=3, column=0)
- tolSentry = Entry(root)
- tolSentry.grid(row=3, column=1)
- tolSentry.insert(0,'1.0')
- Button(root, text="Draw/Redraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
- chkBtnVar = IntVar()
- chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
- chkBtn.grid(row=4, column=0, columnspan=2)
- rawDat1 = regTrees.loadDataSet('sine.txt')
- reDraw.xcord1 = []; reDraw.ycord1 = []
- for i in range(len(rawDat1)):
- reDraw.xcord1.append(rawDat1[i][0]); reDraw.ycord1.append(rawDat1[i][1])
- reDraw.rawDat = mat(rawDat1)
- reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment