Timeseries Prediction Demo base on LSTM
示例代码
import json
import time
import datetime
import requests as req
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense def date2ts(date_str, layout="%Y-%m-%d %H:%M:%S"):
date_struct=time.strptime(date_str, layout)
return int(time.mktime(date_struct)) # 画图之前转换成北京时间
def to_beijing_time(datetime_index):
return datetime_index.tz_localize('UTC').tz_convert('Asia/Shanghai') # 载入原始数据
with open("dataset_2014.json", "r") as rf:
c = rf.read()
d = json.loads(c)
data = {}
for i in range(288*3):
ts = date2ts(d['data']['datetime'][i])
data[ts] = d['data']['count'][i] # 将字典转换为 DataFrame
df = pd.DataFrame(list(data.items()), columns=['timestamp', 'value'])
df['datetime'] = pd.to_datetime(df['timestamp'], unit='s')
df.set_index('datetime', inplace=True)
df.drop('timestamp', axis=1, inplace=True) # 确保数据按时间顺序排序
df = df.sort_index() # 重新采样为 5 分钟间隔,填充缺失值
df = df.resample('5T').mean()
df.interpolate(method='linear', inplace=True) # 数据标准化
scaler = MinMaxScaler(feature_range=(0, 1))
df_scaled = scaler.fit_transform(df)
print("Original Data:\n", df)
print("Scaled Data:\n", df_scaled) # 创建数据集函数
def create_dataset(data, time_step=1):
X, y = [], []
for i in range(len(data) - time_step - 1):
X.append(data[i:(i + time_step), 0])
y.append(data[i + time_step, 0])
return np.array(X), np.array(y) time_step = 10 # 设定用于输入的时间步长
X, y = create_dataset(df_scaled, time_step) # 拆分训练和测试数据集
train_size = int(len(X) * 0.7)
test_size = len(X) - train_size
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:] # 转换为 LSTM 输入格式
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1) # 构建 LSTM 模型
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(time_step, 1)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1)) model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, batch_size=1, epochs=10) # 预测
train_predict = model.predict(X_train)
test_predict = model.predict(X_test) # 反标准化预测结果
train_predict = scaler.inverse_transform(train_predict)
test_predict = scaler.inverse_transform(test_predict) # 反标准化实际值
df_inv_scaled = scaler.inverse_transform(df_scaled)
# print("Inverse Scaled Data:\n", df_inv_scaled) # 创建用于绘制图像的空数组
train_predict_plot = np.empty_like(df_scaled)
train_predict_plot[:, :] = np.nan
train_predict_plot[time_step:len(train_predict) + time_step, :] = train_predict # 计算测试预测结果的起始和结束点
start_point = len(train_predict) + time_step # 设定起始点位置
end_point = start_point + len(test_predict)
test_predict_plot = np.empty_like(df_scaled)
test_predict_plot[:, :] = np.nan
test_predict_plot[start_point:end_point, :] = test_predict[:df_scaled.shape[0] - start_point, :] # 放置到合适位置 # 将时间戳转换回原始时间格式
original = df.index
original_beijing = to_beijing_time(original)
# 创建 figure 绘图
plt.figure(figsize=(15, 6))
plt.title("Timeseries Prediction base on LSTM")
plt.plot(original_beijing, df_inv_scaled, label='True Data') # 确保这里使用适当逆标准化数据
plt.plot(original_beijing, train_predict_plot, label='Train Predict')
plt.plot(original_beijing, test_predict_plot, label='Test Predict')
# 格式化 X 轴,提高可读性
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=12))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M', tz=original_beijing.tz))
plt.gcf().autofmt_xdate(rotation=45)
plt.xlabel('Datetime')
plt.ylabel('Value')
plt.legend()
plt.show() # # 提取最后3天的数据
# last_three_days_mask = original >= (original.max() - pd.Timedelta(days=3))
# filtered_original = original[last_three_days_mask]
# filtered_df_inv_scaled = df_inv_scaled[last_three_days_mask]
# filtered_train_predict_plot = train_predict_plot[last_three_days_mask]
# filtered_test_predict_plot = test_predict_plot[last_three_days_mask]
# # 转换时间为北京时间
# filtered_original_beijing = to_beijing_time(filtered_original) # plt.figure(figsize=(15, 6))
# plt.title("Timeseries Prediction base on LSTM (last 3 days)")
# plt.plot(filtered_original_beijing, filtered_df_inv_scaled, label='True Data') # 确保这里使用适当逆标准化数据
# plt.plot(filtered_original_beijing, filtered_train_predict_plot, label='Train Predict')
# plt.plot(filtered_original_beijing, filtered_test_predict_plot, label='Test Predict')
# # 格式化 X 轴使用年,并倾斜显示
# plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=4))
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M', tz=filtered_original_beijing.tz))
# plt.gcf().autofmt_xdate(rotation=45)
# plt.xlabel('Datetime')
# plt.ylabel('Value')
# plt.legend()
# plt.show()
效果对比
Timeseries Prediction Demo base on LSTM的更多相关文章
- 【转载】Chaotic Time-Series Prediction
原文地址:https://cn.mathworks.com/help/fuzzy/examples/chaotic-time-series-prediction.html?requestedDomai ...
- 用C++调用tensorflow在python下训练好的模型(centos7)
本文主要参考博客https://blog.csdn.net/luoyexuge/article/details/80399265 [1] bazel安装参考:https://blog.csdn.net ...
- 高并发下MySQL出现checking permissions
在某些数据访问层框架中,会使用show full tables from test like 'demo',来检查数据库的状态.当数据库中表的数量较少时,并没有出现严重的问题.但是当数据库中的表数量多 ...
- python中from module import * 的一个陷阱
from module import *把module中的成员全部导到了当前的global namespace,访问起来就比较方便了.当然,python style一般不建议这么做,因为可能引起nam ...
- JavaScript 的性能优化:加载和执行
随着 Web2.0 技术的不断推广,越来越多的应用使用 javascript 技术在客户端进行处理,从而使 JavaScript 在浏览器中的性能成为开发者所面临的最重要的可用性问题.而这个问题又因 ...
- Bootstrap 栅格系统(转载)
源地址:http://www.cnblogs.com/linjiqin/p/3559800.html Bootstrap 栅格系统 目录1.简介2.栅格选项3.列偏移4.嵌套列5.列排序 1.简介Bo ...
- python之import机制
1. 标准 import Python 中所有加载到内存的模块都放在 sys.modules .当 import 一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将 ...
- c++ 虚析构函数[避免内存泄漏]
c++ 虚析构函数: 虚析构函数(1)虚析构函数即:定义声明析构函数前加virtual 修饰, 如果将基类的析构函数声明为虚析构函数时,由该基类所派生的所有派生类的析构函数也都自动成为虚析构函数. ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- CMake 常用方法
CMake 允许开发者编写平台无关的 CMakeLists.txt 文件来定制整个编译流程,然后再根据目标用户的平台进一步生成所需的本地化 Makefile 和工程文件,如 Linux 的Makefi ...
随机推荐
- 【BUG】Python3|安装python3-pip依赖缺失,might want to run ‘apt --fix-broken install‘ to correct these. unment
今天装python,版本装错了. 然后删又删不掉,装pip又装不上,报错是这样的: 想装的时候: 7f2a0f717aa3:~/$ sudo apt-get install python3-pip p ...
- json格式转为List集合
一.JSON格式 {"code":"200","msg":"success","data":[&qu ...
- Python 的 type 及常用魔法方法(上)
魔法方法是 Python 内置方法, 不需要我们手动调用, 它存在的目的是给 解释器 调用的. 比如我们在写 "1 + 1 " 的时候, 这个 "+ " 就会自 ...
- Web前端入门第 53 问:JavaScript 的各种调试方法
任何一门编程语言,在学习之前都应该先弄清楚它的调试方法,毕竟没有不挖坑的人类! 程序一旦出现问题,第一时间就是找到问题出在哪儿,其次才是拿出解决办法.如果都找不到问题原因,那又何从谈起解决办法呢? 如 ...
- 在deepin环境下安装qt开发环境和dtk开发环境
环境 deepinV20.2.2 第一步 进入系统,进入/etc/apt目录 以管理员身份打开(为了编辑源) 第二步 编辑源sources.list 放出第二行源保存并退出 第三步 进入终端,执行su ...
- python 多进程通讯三种方法性能对比(queue, pipe, zeromq)
当然,这三种办法都会在两个进程之间把数据复制一遍,效率肯定没有 shared memory 高,但是考虑到这三种方式都不用考虑锁之类东西,用起来是比较方便的.这三种方式的实现的功能都是差不多的,但是在 ...
- .NET 9中的异常处理性能提升分析:为什么过去慢,未来快
一.为什么要关注.NET异常处理的性能 随着现代云原生.高并发.分布式场景的大量普及,异常处理(Exception Handling)早已不再只是一个冷僻的代码路径.在高复杂度的微服务.网络服务.异步 ...
- 「Log」2023.8.11 小记
序幕 从今天开始记小记. 七点到校了,先小摆一会,然后整理博客. 听 MiTiS 的电音,开始写题. \(\color{blueviolet}{P1829\ [国家集训队]\ Crash的数字表格\ ...
- Go语言中的并发操作
并发与并行 并发:同一时间段内,执行多个任务(你在用微信和两个女朋友聊天) 并行:同一时刻,执行多个任务(你和你朋友都在用微信和女朋友聊天) Go语言中的并发通过goroutine实现.gorouti ...
- 【语义分割专栏】3:Segnet原理篇
目录 前言 背景介绍 Segnet核心剖析 池化索引(pooling Indices) 其他细节 编码器解码器的对称结构 Segnet模型代码 结语 参考资料 前言 本篇文章收录于语义分割专栏,如果对 ...