基于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

相关文章

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

MySQL按时间维度对亿级数据表进行平滑分表

《MySQL按时间维度对亿级数据表进行平滑分表》本文将以一个真实的4亿数据表分表案例为基础,详细介绍如何在不影响线上业务的情况下,完成按时间维度分表的完整过程,感兴趣的小伙伴可以了解一下... 目录引言一、为什么我们需要分表1.1 单表数据量过大的问题1.2 分表方案选型二、分表前的准备工作2.1 数据评估

MySQL中DATE_FORMAT时间函数的使用小结

《MySQL中DATE_FORMAT时间函数的使用小结》本文主要介绍了MySQL中DATE_FORMAT时间函数的使用小结,用于格式化日期/时间字段,可提取年月、统计月份数据、精确到天,对大家的学习或... 目录前言DATE_FORMAT时间函数总结前言mysql可以使用DATE_FORMAT获取日期字段

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间