留全部代码备份
通过facebook开源模型Prophet对未来时间内某基坑变形监控值进行预测,但该模型好像并不适用于这种施工过程中的数据预测,但是至少能预测,交差总没问题吧。预测10天。
import pandas as pd
from matplotlib import pyplot as plt
from fbprophet import Prophet
import numpy as np
import matplotlib
import tkinter as tk
from tkinter import ttk
from matplotlib.pylab import mpl
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk
from mpldatacursor import datacursor
import datetime
import time
import threading
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False
def forecast(ax2):# 用来正常显示负号df = pd.read_csv('data.csv')df.head()m = Prophet(daily_seasonality=True)m.fit(df)future = m.make_future_dataframe(periods=10)future.tail()forecast = m.predict(future)forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()alltime = forecast['ds']histtime = m.history['ds']futuretime = alltime[m.params['Y'].size:]alldata = forecast['yhat']histdata = m.history['y']futuredata = alldata[m.params['Y'].size:]xlabel='日期'ylabel='监测值'figsize=(10, 6)fig = plt.figure(facecolor='w', figsize=figsize)ax2.clear()line1, = ax2.plot(histtime, histdata, marker='+',ls='-', c='#0072B2')ax2.plot([histtime[histtime.size-1],futuretime[histtime.size]], [histdata[histdata.size-1],futuredata[histdata.size]], ls='-', c='#F072B2')line2, = ax2.plot(futuretime, futuredata, marker='+',ls='-', c='#F072B2')ax2.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)ax2.set_xlabel(xlabel)ax2.set_ylabel(ylabel)ax2.legend(handles = [line1, line2,], labels = ['历史值', '预测值'], loc = 'best')fig.tight_layout()return [fig,[histtime,histdata,futuretime,futuredata]]def update_tk(tk_root,ax,canvas):tk_root.title('预测计算中...')[figure,data] = forecast(ax)canvas.draw_idle()fm2 =tk_root.children['!frame2']fm21 = fm2.children['!frame']scrollBar22 = fm21.children['!scrollbar']treehist = fm21.children['!treeview']histtime = np.array(data[0],dtype=np.datetime64).tolist()for i in range(len(histtime)):treehist.insert("",'end',text="" ,values=(i+1,time.strftime("%Y-%m-%d",time.localtime(histtime[i]/1000000000)),data[1][i])) #插入数据,fm22 = fm2.children['!frame2']treefutrue = fm22.children['!treeview']scrollBar22 = fm22.children['!scrollbar']futuretime = np.array(data[2],dtype=np.datetime64).tolist()futuredata = np.array(data[3],dtype=np.float64).tolist()for i in range(len(futuretime)):treefutrue.insert("",'end',text="" ,values=(i+1,time.strftime("%Y-%m-%d",time.localtime(futuretime[i]/1000000000)),futuredata[i])) #插入数据,scrollBar22.config(command=treefutrue.yview)tk_root.title('检测值预测完成')if __name__ == '__main__':root = tk.Tk()root.title('检测值预测窗口')fm1 = tk.Frame(root)# 进入消息循环xlabel='日期'ylabel='监测值'figsize=(10, 6)figure = plt.figure(facecolor='w', figsize=figsize)ax2 = figure.add_subplot(111)ax2.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)ax2.set_xlabel(xlabel)ax2.set_ylabel(ylabel)figure.tight_layout()canvas=FigureCanvasTkAgg(figure,fm1)canvas.draw() #以前的版本使用show()方法,matplotlib 2.2之后不再推荐show()用draw代替,但是用show不会报错,会显示警告canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)#把matplotlib绘制图形的导航工具栏显示到tkinter窗口上toolbar =NavigationToolbar2Tk(canvas, fm1) #matplotlib 2.2版本之后推荐使用NavigationToolbar2Tk,若使用NavigationToolbar2TkAgg会警告toolbar.update()canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)fm1.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.YES)fm2 = tk.Frame(root)fm21 = tk.Frame(fm2)scrollBar21 = tk.Scrollbar(fm21)scrollBar21.pack(side=tk.RIGHT, fill=tk.Y)treehist=ttk.Treeview(fm21,show="headings",yscrollcommand=scrollBar21.set)#表格treehist["columns"]=("id","time","data")treehist.column("id",width=30) #表示列,不显示treehist.column("time",width=100) #表示列,不显示treehist.column("data",width=100)treehist.heading("id",text="序号") #显示表头 treehist.heading("time",text="历史日期") #显示表头treehist.heading("data",text="历史监测值")histtime = []histdata = []for i in range(len(histtime)):treehist.insert("",'end',text="" ,values=(i+1,time.strftime("%Y-%m-%d",time.localtime(histtime[i]/1000000000)),histdata[i])) #插入数据,treehist.pack(side=tk.TOP, fill=tk.BOTH, expand=1)fm21.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.YES)scrollBar21.config(command=treehist.yview)fm22 = tk.Frame(fm2)scrollBar22 = tk.Scrollbar(fm22)scrollBar22.pack(side=tk.RIGHT, fill=tk.Y)treefutrue=ttk.Treeview(fm22,show="headings",yscrollcommand=scrollBar22.set)#表格treefutrue["columns"]=("id","time","data")treefutrue.column("id",width=30) #表示列,不显示treefutrue.column("time",width=100) #表示列,不显示treefutrue.column("data",width=100)treefutrue.heading("id",text="序号") #显示表头 treefutrue.heading("time",text="预测日期") #显示表头treefutrue.heading("data",text="预测监测值")futuretime = []futuredata = []for i in range(len(futuretime)):treefutrue.insert("",'end',text="" ,values=(i+1,time.strftime("%Y-%m-%d",time.localtime(futuretime[i]/1000000000)),futuredata[i])) #插入数据,treefutrue.pack(side=tk.TOP, fill=tk.BOTH, expand=1)fm22.pack(side=tk.RIGHT, fill=tk.BOTH, expand=tk.YES)scrollBar22.config(command=treefutrue.yview)fm2.pack(side=tk.RIGHT, fill=tk.BOTH, expand=tk.YES)th=threading.Thread(target=update_tk,args=(root,ax2,canvas,)) th.setDaemon(True)#守护线程 th.start() root.mainloop()
结果如下图: