lolamontes69

treeExplorerRewrite.py for MLIA Chapter 9

Nov 27th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. """ treeExplorer.py for Machine Learning in Action, Chapter 9.
  2.  
  3.    I was getting errors galore using _tkagg so I just split
  4.    the gui into two to get a working example.
  5.               --lolamontes69
  6. """
  7. from numpy import *
  8. from Tkinter import *
  9. import regTrees
  10. import matplotlib.pyplot as plt
  11.  
  12. def reDraw(tolS,tolN):
  13.     f = plt.figure('Tree Explorer - sine.txt')
  14.     f.clf()
  15.     a = f.add_subplot(111)
  16.     if chkBtnVar.get():
  17.         if tolN < 2: tolN = 2
  18.         myTree=regTrees.createTree(reDraw.rawDat, regTrees.modelLeaf,\
  19.                                    regTrees.modelErr, (tolS,tolN))
  20.         yHat = regTrees.createForeCast(myTree, reDraw.testDat,regTrees.modelTreeEval)
  21.     else:
  22.         myTree=regTrees.createTree(reDraw.rawDat, ops=(tolS,tolN))
  23.         yHat = regTrees.createForeCast(myTree, reDraw.testDat)
  24.     a.scatter(reDraw.xcord1, reDraw.ycord1, s=5)
  25.     a.plot(reDraw.testDat, yHat, linewidth=2.0)
  26.     plt.show()
  27.  
  28.  
  29. def getInputs():
  30.     try: tolN = int(tolNentry.get())
  31.     except:
  32.         tolN = 10
  33.         print "enter Integer for tolN"
  34.         tolNentry.delete(0, END)
  35.         tolNentry.insert(0,'10')
  36.     try: tolS = float(tolSentry.get())
  37.     except:
  38.         tolS = 1.0
  39.         print "enter Float for tolS"
  40.         tolSentry.delete(0, END)
  41.         tolSentry.insert(0,'1.0')
  42.     return tolN,tolS
  43.  
  44. def drawNewTree():
  45.     tolN,tolS = getInputs()
  46.     reDraw(tolS,tolN)
  47.  
  48. root=Tk()
  49. root.title("Tree Explorer - sine.txt")
  50. text1 = "\nUSING TREE EXPLORER.\n\n\
  51.         tolN and tolS are used for pre-pruning the tree.                      \n\
  52.         tolN - minimum number of data instances to include in tree splits     \n\
  53.         tolS - a tolerance on the error reduction                             \n\
  54.                                                                               \n\
  55.         To draw the first plot;                                               \n\
  56.           1: press the <Draw/Redraw> button                                   \n\
  57.                                                                               \n\
  58.         To try new tolN and tolS values;                                      \n\
  59.           1: close the plot                                                   \n\
  60.           2: enter the new values in the boxes below                          \n\
  61.           3: press <Draw/Redraw>                                              \n\n"
  62. Label(root, text=text1).grid(row=0, column=0, columnspan=4)
  63. Label(root, text="tolN").grid(row=2, column=0)
  64. tolNentry = Entry(root)
  65. tolNentry.grid(row=2, column=1)
  66. tolNentry.insert(0,'10')
  67. Label(root, text="tolS").grid(row=3, column=0)
  68. tolSentry = Entry(root)
  69. tolSentry.grid(row=3, column=1)
  70. tolSentry.insert(0,'1.0')
  71. Button(root, text="Draw/Redraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
  72. chkBtnVar = IntVar()
  73. chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
  74. chkBtn.grid(row=4, column=0, columnspan=2)
  75.  
  76. rawDat1 = regTrees.loadDataSet('sine.txt')
  77.  
  78. reDraw.xcord1 = []; reDraw.ycord1 = []
  79. for i in range(len(rawDat1)):
  80.     reDraw.xcord1.append(rawDat1[i][0]); reDraw.ycord1.append(rawDat1[i][1])
  81.  
  82. reDraw.rawDat = mat(rawDat1)
  83. reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)
  84.  
  85. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment