Python制作數(shù)據(jù)預測集成工具(值得收藏)
大數(shù)據(jù)預測是大數(shù)據(jù)最核心的應(yīng)用,是它將傳統(tǒng)意義的預測拓展到“現(xiàn)測”。大數(shù)據(jù)預測的優(yōu)勢體現(xiàn)在,它把一個非常困難的預測問題,轉(zhuǎn)化為一個相對簡單的描述問題,而這是傳統(tǒng)小數(shù)據(jù)集根本無法企及的。從預測的角度看,大數(shù)據(jù)預測所得出的結(jié)果不僅僅是用于處理現(xiàn)實業(yè)務(wù)的簡單、客觀的結(jié)論,更是能用于幫助企業(yè)經(jīng)營的決策。
在過去,人們的決策主要是依賴 20% 的結(jié)構(gòu)化數(shù)據(jù),而大數(shù)據(jù)預測則可以利用另外 80% 的非結(jié)構(gòu)化數(shù)據(jù)來做決策。大數(shù)據(jù)預測具有更多的數(shù)據(jù)維度,更快的數(shù)據(jù)頻度和更廣的數(shù)據(jù)寬度。與小數(shù)據(jù)時代相比,大數(shù)據(jù)預測的思維具有 3 大改變:實樣而非抽樣;預測效率而非精確;相關(guān)關(guān)系而非因果關(guān)系。
而今天我們就將利用python制作可視化的大數(shù)據(jù)預測部分集成工具,其中數(shù)據(jù)在這里使用一個實驗中的數(shù)據(jù)。普遍性的應(yīng)用則直接從文件讀取即可。其中的效果圖如下:
實驗前的準備
首先我們使用的python版本是3.6.5所用到的模塊如下:
- sklearn模塊用來創(chuàng)建整個模型訓練和保存調(diào)用以及算法的搭建框架等等。
- numpy模塊用來處理數(shù)據(jù)矩陣運算。
- matplotlib模塊用來可視化擬合模型效果。
- Pillow庫用來加載圖片至GUI界面。
- Pandas模塊用來讀取csv數(shù)據(jù)文件。
- Tkinter用來創(chuàng)建GUI窗口程序。
數(shù)據(jù)的訓練和訓練的GUI窗口
經(jīng)過算法比較,發(fā)現(xiàn)這里我們選擇使用sklearn簡單的多元回歸進行擬合數(shù)據(jù)可以達到比較好的效果。
(1)首先是是數(shù)據(jù)的讀取,通過設(shè)定選定文件夾函數(shù)來讀取文件,加載數(shù)據(jù)的效果:
'''選擇文件功能''' def selectPath(): # 選擇文件path_接收文件地址 path_ =tkinter.filedialog.askopenfilename() # 通過replace函數(shù)替換絕對文件地址中的/來使文件可被程序讀取 # 注意:\\轉(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 # 讀取已使用年限作為標簽 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() #標簽名稱位置 label1=tk.Label(text = "目標路徑:") label1.pack() e1=tk.Entry( textvariable = path) e1.pack() bn1=tk.Button(text = "路徑選擇", command = selectPath) bn1.pack() bn2=tk.Button(text = "模型訓練", command = train) bn2.pack() bn3=tk.Button(text = "模型預測", command = test) bn3.pack() #標簽按鈕等放在背景圖上 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()
效果如下可見:
(2)然后是數(shù)據(jù)的擬合和可視化模型效果:
# 模型擬合 reg = LinearRegression() reg.fit(X, Y) # 預測效果 predict = reg.predict(np.array([X[0]])) Y_predict = reg.predict(X) print(Y_predict) # 橫坐標 x_label = [] for i in range(len(Y)): x_label.append(i) # 繪圖 fig, ax = plt.subplots() # 真實值分布散點圖 plt.scatter(x_label, Y) # 預測值分布散點圖 plt.scatter(x_label, Y_predict) # 預測值擬合直線圖 plt.plot(x_label, Y_predict) # 橫縱坐標 ax.set_xlabel('預測值與真實值模型擬合效果圖') ax.set_ylabel('藍色為真實值,黃色為預測值') # 將繪制的圖形顯示到tkinter:創(chuàng)建屬于root的canvas畫布,并將圖f置于畫布上 canvas = FigureCanvasTkAgg(fig, master=root) canvas.draw() # 注意show方法已經(jīng)過時了,這里改用draw canvas.get_tk_widget().pack() # matplotlib的導航工具欄顯示上來(默認是不會顯示它的) toolbar = NavigationToolbar2Tk(canvas, root) toolbar.update() canvas._tkcanvas.pack() #彈窗顯示 messagebox.showinfo(title='模型情況', message="模型訓練完成!") 其中的效果如下可見:
其中的效果如下可見:
模型的預測和使用
其中模型的預測主要通過兩種方式進行預測,分別是:手動輸入單個數(shù)據(jù)進行預測和讀取文件進行預測。
其中手動輸入數(shù)據(jù)進行預測需要設(shè)置更多的GUI按鈕,其中代碼如下:
#子窗口 LOVE = Toplevel(root) LOVE.geometry("+100+260") LOVE.title = "模型測試" #子窗口各標簽名 label = ["上升沿斜率(v/us)", "下降沿斜率(v/us)", "脈寬(ns)", "低狀態(tài)電平(mv)", "低電平方差(mv2)x10-3", "高狀態(tài)電平(v)", "高電平方差(v2)", "信號質(zhì)量因子"] Label(LOVE, text="1、輸入?yún)?shù)預測", font=("微軟雅黑", 20)).grid(row=0, column=0) #標簽名稱,字體位置 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) #測試輸入框預測 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())]) # 預測效果 predict = reg.predict(np.array([x])) Label(LOVE, text="預測結(jié)果已使用年數(shù)為:"+str(predict[0])+"年", font=("微軟雅黑", 10)).grid(row=4, column=3) print(predict) Button(LOVE, text="預測:", font=("微軟雅黑", 15),command=pp).grid(row=4, column=0) Label(LOVE, text="2、選擇文件預測", font=("微軟雅黑", 20)).grid(row=5, column=0) path1 = StringVar() label1 = tk.Label(LOVE,text="目標路徑:", font=("微軟雅黑", 10)) label1.grid(row=6, column=0) e1 = tk.Entry(LOVE,textvariable=path1, font=("微軟雅黑", 10)) e1.grid(row=6, column=2) label = ["上升沿斜率(v/us)", "下降沿斜率(v/us)", "脈寬(ns)", "低狀態(tài)電平(mv)", "低電平方差(mv2)x10-3", "高狀態(tài)電平(v)", "高電平方差(v2)", "信號質(zhì)量因子"] n = 0 for i in predict_value: print(str(label) + "分別為" + str(X[n]) + "預測出來的結(jié)果為:" + str(i) + "年" + "\n") f = open("預測結(jié)果.txt", "a") f.write(str(label) + "分別為" + str(X[n]) + "預測出來的結(jié)果為:" + str(i) + "年" + "\n") f.close() f = open("result.txt", "a") f.write(str(i) + "\n") f.close() n += 1 messagebox.showinfo(title='模型情況', message="預測結(jié)果保存在當前文件夾下的TXT文件中!") os.system("result.txt") os.system("預測結(jié)果.txt") Button(LOVE, text="預測:", font=("微軟雅黑", 15), command=ppt).grid(row=7, column=0)
效果如下可見:
選擇文件進行讀取預測和模型訓練數(shù)據(jù)的讀取類似,代碼如下:
#選擇文件預測 def selectPath1(): # 選擇文件path_接收文件地址 path_ =tkinter.filedialog.askopenfilename() # 通過replace函數(shù)替換絕對文件地址中的/來使文件可被程序讀取 # 注意:\\轉(zhuǎn)義后為\,所以\\\\轉(zhuǎn)義后為\\ path_ =path_.replace("/", "\\\\") # path設(shè)置path_的值 path1.set(path_) return path bn1 = tk.Button(LOVE,text="路徑選擇", font=("微軟雅黑", 10), command=selectPath1) bn1.grid(row=6, column=6) def ppt(): try: os.remove("預測結(jié)果.txt") os.remove("result.txt") except: pass # 文件的名字 FILENAME =path1.get() # 禁用科學計數(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)
效果如下:
由于讀取文件進行預測的話,數(shù)據(jù)較多故直接存儲在TXT中方便查看
以上就是Python制作數(shù)據(jù)預測集成工具(值得收藏)的詳細內(nèi)容,更多關(guān)于python 數(shù)據(jù)預測的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Cython中prange函數(shù)實現(xiàn)for循環(huán)的并行
Cython中提供了一個prange函數(shù),專門用于循環(huán)的并行執(zhí)行。這個 prange的特殊功能是Cython獨一無二的,并且prange只能與for循環(huán)搭配使用,不能獨立存在。本文就將使用 prange 實現(xiàn) for 循環(huán)的并行,感興趣的可以了解一下2022-08-08python 使用xlsxwriter循環(huán)向excel中插入數(shù)據(jù)和圖片的操作
這篇文章主要介紹了python 使用xlsxwriter循環(huán)向excel中插入數(shù)據(jù)和圖片的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Python局部函數(shù)及用法詳解(含nonlocal關(guān)鍵字)
局部函數(shù)有哪些特征,在使用時需要注意什么呢?接下來就給讀者詳細介紹?Python?局部函數(shù)的用法,對Python局部函數(shù)相關(guān)知識感興趣的朋友跟隨小編一起看看吧2022-12-12pandas創(chuàng)建DataFrame的7種方法小結(jié)
這篇文章主要介紹了pandas創(chuàng)建DataFrame的7種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06