基于Prophet时间序列的监测值预测

2024-02-10 16:32

本文主要是介绍基于Prophet时间序列的监测值预测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 留全部代码备份

通过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() 

结果如下图:

 

这篇关于基于Prophet时间序列的监测值预测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/697564

相关文章

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

如何使用 Bash 脚本中的time命令来统计命令执行时间(中英双语)

《如何使用Bash脚本中的time命令来统计命令执行时间(中英双语)》本文介绍了如何在Bash脚本中使用`time`命令来测量命令执行时间,包括`real`、`user`和`sys`三个时间指标,... 使用 Bash 脚本中的 time 命令来统计命令执行时间在日常的开发和运维过程中,性能监控和优化是不

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit