日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

树回归--python Tkinter库创建GUI(2)

發布時間:2024/9/20 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树回归--python Tkinter库创建GUI(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單的Tkinter庫創建GUI的例子可參考:
http://blog.csdn.net/lilong117194/article/details/78456376

下面是代碼:

# -*- coding: utf-8 -*- """ Created on Mon Nov 06 10:24:42 2017""" from numpy import * from Tkinter import * import regTrees import matplotlib # 導入matplot文件 matplotlib.use('TkAgg') # 設置后端TkAgg # 將TkAgg和matplotlib圖鏈接起來 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure# 繪制樹 def reDraw(tolS,tolN): reDraw.f.clf() # 清空之前的圖像reDraw.a = reDraw.f.add_subplot(111) # 各個子圖像也被清除,重新添加新圖if chkBtnVar.get(): # 檢查復選框是否被選中if tolN < 2: tolN = 2myTree=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)reDraw.a.scatter(reDraw.rawDat[:,0], reDraw.rawDat[:,1], s=5) # 繪制真實值reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) # 繪制預測值reDraw.canvas.show()def getInputs(): # 獲取用戶輸入try: tolN = int(tolNentry.get()) # 期望輸入是整數,在Entry部件上調用.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) # 調用reDraw繪制圖# 布局GUI代碼 root=Tk()# 創建一個TK類型的根部件 reDraw.f = Figure(figsize=(5,4), dpi=100) # 創建畫布 reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root) reDraw.canvas.show() reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)Label(root, text="tolN").grid(row=1, column=0) # grid方法設置行和列的位置 tolNentry = Entry(root) # Entry:允許單行輸入的文本輸入框 tolNentry.grid(row=1, column=1) # 設置文本框的位置 tolNentry.insert(0,'10') Label(root, text="tolS").grid(row=2, column=0) tolSentry = Entry(root) # 文本輸入框放在root中 tolSentry.grid(row=2, column=1) tolSentry.insert(0,'1.0') # 點擊ReDraw按鈕時,就調用drawNewTree()函數 Button(root, text="ReDraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)chkBtnVar = IntVar() # 按鈕整數值,為了讀取Checkbutton的值 chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar) # Checkbutton復選按鈕 chkBtn.grid(row=3, column=0, columnspan=2)reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt')) reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01) reDraw(1.0, 10)root.mainloop()

運行結果:

分類回歸樹:

模型樹:

構建盡可能大的樹:

注意:這里的import regTrees用的是《機器學習實戰》里的源碼,但是有兩處需要改動的地方,具體查看:
http://blog.csdn.net/lilong117194/article/details/78444615

通過GUI可以更友好的調節參數和模型來訓練模型和學習算法。。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的树回归--python Tkinter库创建GUI(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。