溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具

發(fā)布時(shí)間:2021-07-05 17:54:46 來(lái)源:億速云 閱讀:205 作者:Leah 欄目:編程語(yǔ)言

這篇文章給大家介紹Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

數(shù)據(jù)的訓(xùn)練和訓(xùn)練的GUI窗口

經(jīng)過(guò)算法比較,發(fā)現(xiàn)這里我們選擇使用sklearn簡(jiǎn)單的多元回歸進(jìn)行擬合數(shù)據(jù)可以達(dá)到比較好的效果。

(1)首先是是數(shù)據(jù)的讀取,通過(guò)設(shè)定選定文件夾函數(shù)來(lái)讀取文件,加載數(shù)據(jù)的效果:

'''選擇文件功能''' def selectPath():     # 選擇文件path_接收文件地址     path_ =tkinter.filedialog.askopenfilename()     # 通過(guò)replace函數(shù)替換絕對(duì)文件地址中的/來(lái)使文件可被程序讀取     # 注意:\\轉(zhuǎn)義后為\,所以\\\\轉(zhuǎn)義后為\\     path_ =path_.replace("/", "\\\\")     # path設(shè)置path_的值     path.set(path_)     return path  # 得到的DataFrame讀入所有數(shù)據(jù) data = pd.read_excel(FILENAME, header=0, usecols="A,B,C,D,E,F,G,H,I") # DataFrame轉(zhuǎn)化為array DataArray = data.values # 讀取已使用年限作為標(biāo)簽 Y = DataArray[:, 8] # 讀取其他參數(shù)作為自變量,影響因素 X = DataArray[:, 0:8] # 字符串轉(zhuǎn)變?yōu)檎麛?shù) for i in range(len(Y)):     Y[i] = int(Y[i].replace("年", "")) X = np.array(X)  # 轉(zhuǎn)化為array Y = np.array(Y)  # 轉(zhuǎn)化為array  root = Tk() root.geometry("+500+260") # 背景圖設(shè)置 canvas = tk.Canvas(root, width=600, height=200, bd=0, highlightthickness=0) imgpath = '1.jpg' img = Image.open(imgpath) photo = ImageTk.PhotoImage(img) #背景圖大小設(shè)置 canvas.create_image(700, 400, image=photo) canvas.pack() path = StringVar() #標(biāo)簽名稱位置 label1=tk.Label(text = "目標(biāo)路徑:") label1.pack() e1=tk.Entry( textvariable = path) e1.pack() bn1=tk.Button(text = "路徑選擇", command = selectPath) bn1.pack() bn2=tk.Button(text = "模型訓(xùn)練", command = train) bn2.pack() bn3=tk.Button(text = "模型預(yù)測(cè)", command = test) bn3.pack() #標(biāo)簽按鈕等放在背景圖上 canvas.create_window(50, 50, width=150, height=30,                      window=label1) canvas.create_window(280, 50, width=300, height=30,                      window=e1) canvas.create_window(510, 50, width=150, height=30,                      window=bn1) canvas.create_window(50, 100, width=150, height=30,                      window=bn2) canvas.create_window(510, 100, width=150, height=30,                      window=bn3)  root.mainloop()

效果如下可見(jiàn):

Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具

(2)然后是數(shù)據(jù)的擬合和可視化模型效果:

# 模型擬合 reg = LinearRegression() reg.fit(X, Y) # 預(yù)測(cè)效果 predict = reg.predict(np.array([X[0]])) Y_predict = reg.predict(X) print(Y_predict) # 橫坐標(biāo) x_label = [] for i in range(len(Y)):     x_label.append(i) # 繪圖 fig, ax = plt.subplots() # 真實(shí)值分布散點(diǎn)圖 plt.scatter(x_label, Y) # 預(yù)測(cè)值分布散點(diǎn)圖 plt.scatter(x_label, Y_predict) # 預(yù)測(cè)值擬合直線圖 plt.plot(x_label, Y_predict) # 橫縱坐標(biāo) ax.set_xlabel('預(yù)測(cè)值與真實(shí)值模型擬合效果圖') ax.set_ylabel('藍(lán)色為真實(shí)值,黃色為預(yù)測(cè)值') # 將繪制的圖形顯示到tkinter:創(chuàng)建屬于root的canvas畫布,并將圖f置于畫布上 canvas = FigureCanvasTkAgg(fig, master=root) canvas.draw()  # 注意show方法已經(jīng)過(guò)時(shí)了,這里改用draw canvas.get_tk_widget().pack() # matplotlib的導(dǎo)航工具欄顯示上來(lái)(默認(rèn)是不會(huì)顯示它的) toolbar = NavigationToolbar2Tk(canvas, root) toolbar.update() canvas._tkcanvas.pack() #彈窗顯示 messagebox.showinfo(title='模型情況', message="模型訓(xùn)練完成!") 其中的效果如下可見(jiàn):

其中的效果如下可見(jiàn):

Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具

模型的預(yù)測(cè)和使用

其中模型的預(yù)測(cè)主要通過(guò)兩種方式進(jìn)行預(yù)測(cè),分別是:手動(dòng)輸入單個(gè)數(shù)據(jù)進(jìn)行預(yù)測(cè)和讀取文件進(jìn)行預(yù)測(cè)。

其中手動(dòng)輸入數(shù)據(jù)進(jìn)行預(yù)測(cè)需要設(shè)置更多的GUI按鈕,其中代碼如下:

#子窗口 LOVE = Toplevel(root) LOVE.geometry("+100+260") LOVE.title = "模型測(cè)試" #子窗口各標(biāo)簽名 label = ["上升沿斜率(v/us)", "下降沿斜率(v/us)", "脈寬(ns)", "低狀態(tài)電平(mv)", "低電平方差(mv2)x10-3", "高狀態(tài)電平(v)", "高電平方差(v2)", "信號(hào)質(zhì)量因子"] Label(LOVE, text="1、輸入?yún)?shù)預(yù)測(cè)", font=("微軟雅黑", 20)).grid(row=0, column=0) #標(biāo)簽名稱,字體位置 Label(LOVE, text=label[0], font=("微軟雅黑",10)).grid(row=1, column=0) Label(LOVE, text=label[1], font=("微軟雅黑", 10)).grid(row=1, column=1) Label(LOVE, text=label[2], font=("微軟雅黑", 10)).grid(row=1, column=2) Label(LOVE, text=label[3], font=("微軟雅黑", 10)).grid(row=1, column=3) Label(LOVE, text=label[4], font=("微軟雅黑", 10)).grid(row=1, column=4) Label(LOVE, text=label[5], font=("微軟雅黑", 10)).grid(row=1, column=5) Label(LOVE, text=label[6], font=("微軟雅黑", 10)).grid(row=1, column=6) Label(LOVE, text=label[7], font=("微軟雅黑", 10)).grid(row=1, column=7) #編輯框位置和字體 en1=tk.Entry(LOVE, font=("微軟雅黑", 8)) en1.grid(row=2, column=0) en2=tk.Entry(LOVE, font=("微軟雅黑", 8)) en2.grid(row=2, column=1) en3=tk.Entry(LOVE, font=("微軟雅黑", 8)) en3.grid(row=2, column=2) en4=tk.Entry(LOVE, font=("微軟雅黑", 8)) en4.grid(row=2, column=3) en5=tk.Entry(LOVE, font=("微軟雅黑", 8)) en5.grid(row=2, column=4) en6=tk.Entry(LOVE, font=("微軟雅黑", 8)) en6.grid(row=2, column=5) en7=tk.Entry(LOVE, font=("微軟雅黑", 8)) en7.grid(row=2, column=6) en8=tk.Entry(LOVE, font=("微軟雅黑", 8)) en8.grid(row=2, column=7) Label(LOVE, text="", font=("微軟雅黑", 10)).grid(row=3, column=0) #測(cè)試輸入框預(yù)測(cè) def pp():     x=np.array([int(en1.get()),int(en2.get()),int(en3.get()),int(en4.get()),int(en5.get()),int(en6.get()),int(en7.get()),int(en8.get())])     # 預(yù)測(cè)效果     predict = reg.predict(np.array([x]))     Label(LOVE, text="預(yù)測(cè)結(jié)果已使用年數(shù)為:"+str(predict[0])+"年", font=("微軟雅黑", 10)).grid(row=4, column=3)     print(predict) Button(LOVE, text="預(yù)測(cè):", font=("微軟雅黑", 15),command=pp).grid(row=4, column=0) Label(LOVE, text="2、選擇文件預(yù)測(cè)", font=("微軟雅黑", 20)).grid(row=5, column=0) path2 = StringVar() label1 = tk.Label(LOVE,text="目標(biāo)路徑:", font=("微軟雅黑", 10)) label1.grid(row=6, column=0) e1 = tk.Entry(LOVE,textvariable=path2, font=("微軟雅黑", 10)) e1.grid(row=6, column=2) label = ["上升沿斜率(v/us)", "下降沿斜率(v/us)", "脈寬(ns)", "低狀態(tài)電平(mv)", "低電平方差(mv2)x10-3", "高狀態(tài)電平(v)", "高電平方差(v2)",              "信號(hào)質(zhì)量因子"]     n = 0     for i in predict_value:         print(str(label) + "分別為" + str(X[n]) + "預(yù)測(cè)出來(lái)的結(jié)果為:" + str(i) + "年" + "\n")         f = open("預(yù)測(cè)結(jié)果.txt", "a")         f.write(str(label) + "分別為" + str(X[n]) + "預(yù)測(cè)出來(lái)的結(jié)果為:" + str(i) + "年" + "\n")         f.close()         f = open("result.txt", "a")         f.write(str(i) + "\n")         f.close()         n += 1     messagebox.showinfo(title='模型情況', message="預(yù)測(cè)結(jié)果保存在當(dāng)前文件夾下的TXT文件中!")     os.system("result.txt")     os.system("預(yù)測(cè)結(jié)果.txt") Button(LOVE, text="預(yù)測(cè):", font=("微軟雅黑", 15), command=ppt).grid(row=7, column=0)

效果如下可見(jiàn):

Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具

選擇文件進(jìn)行讀取預(yù)測(cè)和模型訓(xùn)練數(shù)據(jù)的讀取類似,代碼如下:

#選擇文件預(yù)測(cè) def selectPath2():     # 選擇文件path_接收文件地址     path_ =tkinter.filedialog.askopenfilename()     # 通過(guò)replace函數(shù)替換絕對(duì)文件地址中的/來(lái)使文件可被程序讀取     # 注意:\\轉(zhuǎn)義后為\,所以\\\\轉(zhuǎn)義后為\\     path_ =path_.replace("/", "\\\\")     # path設(shè)置path_的值     path2.set(path_)     return path bn1 = tk.Button(LOVE,text="路徑選擇", font=("微軟雅黑", 10), command=selectPath2) bn1.grid(row=6, column=6) def ppt():     try:         os.remove("預(yù)測(cè)結(jié)果.txt")         os.remove("result.txt")     except:         pass     # 文件的名字     FILENAME =path2.get()     # 禁用科學(xué)計(jì)數(shù)法     pd.set_option('float_format', lambda x: '%.3f' % x)     np.set_printoptions(threshold=np.inf)     # 得到的DataFrame讀入所有數(shù)據(jù)     data =pd.read_excel(FILENAME, header=0, usecols="A,B,C,D,E,F,G,H")     # DataFrame轉(zhuǎn)化為array     DataArray =data.values     # 讀取其他參數(shù)作為自變量,影響因素     X = DataArray[:,0:8]     predict_value = reg.predict(X)     print(predict_value)

效果如下:

Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具

由于讀取文件進(jìn)行預(yù)測(cè)的話,數(shù)據(jù)較多故直接存儲(chǔ)在TXT中方便查看:

Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具

關(guān)于Python中怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)預(yù)測(cè)集成工具就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI