LSTM时间序列预测研究
基于LSTM的时间序列预测方法,利用Tensorflow和Keras框架,涵盖单变量、多变量及多步预测。完整流程包括数据清洗、特征提取、建模与预测,并通过空气质量、洗发水销量等案例验证了方案的有效性与健壮性。
LSTM 时间序列分析预测 目录
概述
时间序列预测是经典数据分析任务,采用LSTM神经网络进行处理能够显著提升预测效果。本文基于TensorFlow框架与Keras接口,完整展示项目开发流程:数据清洗、特征提取、模型构建及预测。经过多次验证的方案可直接应用于实际项目。
项目资源
自回归(AR、ARIMA)模型时间序列预测合集 · 深度学习模型时间序列预测合集 · 基于NLP的文本分析项目合集
第一部分:基础LSTM应用
一、LSTM单变量预测(shampoo-sales)
基于香皂销售数据的单变量基础案例,涵盖数据预处理、观测值缩放、时间序列平稳化、监督学习数据转换、模型开发、LSTM模型构建、完整案例实现及健壮性优化。
二、LSTM多变量预测(air_pollution)
多变量数据输出方法、预处理流程、模型定义与训练步骤均有详细说明。
三、Multi-Step LSTM预测
静态模型进行多步预测的实现思路,详细介绍LSTM网络如何完成多步预测。
第二部分:LSTM进阶应用(airline-passengers)
从最简单的LSTM回归网络(1→1)开始,逐步过渡到移动窗口型回归(3→1)、时间步长型回归(3→1),再深入批次间记忆LSTM、批次间堆叠LSTM,实现层层递进。
第三部分:LSTM核心特性
一、编码器-解码器架构
以回声随机序列为例,介绍数据准备、序列预测模型实现、简化版可观测数据输入输出模式。涵盖一对一LSTM、多对一LSTM、多对多LSTM(TimeDistributed)、有状态网络预测输入输出对配置、数据重塑方法,完整实现案例。
二、Keras LSTM生命周期
按照5步操作流程,解析代码实现,步骤清晰。
第四部分:数据准备技术
一、缺失值处理
序列缺失值处理方法包括:忽略缺失值策略、删除缺失数据、替换缺失数据,各自适用不同场景。
二、数据标准化
标准化方法与归一化方法的选择需根据数据分布决定。
三、数据变换
差分消除季节性、差分消除趋势是两种实用技术。
四、特征编码
One-hot编码实现方式包括Keras实现、Scikit-learn实现、手动实现,覆盖常见场景。
五、数据重塑
单输入样本处理、多输入特征处理、单变量时间序列准备,数据重塑是后续建模的基础。
第五部分:LSTM建模技术
一、网络架构
堆叠LSTM实现,2D输出版本和3D输出版本各有侧重。
二、模型管理
模型保存与加载是工程化基础。
三、模型诊断
欠拟合识别(训练周期不足)、合格模型标准、过拟合识别、多次拟合评估方法,帮助判断模型性能。
第六部分:完整案例
案例1:空气质量预测(多变量)
包含数据准备与可视化、监督学习数据转换、单日预测模型、三日预测模型,全面理解多变量预测流程。
案例2:洗发水销量(单步预测)
涵盖数据集分析、滞后模型构建、监督数据结构、差分法与缩放法、LSTM实现与评估,并加入股票数据测试、原始数据预测、验证集损失分析。
案例3:洗发水销量(多步预测)
监督数据准备、静态预测效果、神经网络预测实现,规避多步预测常见问题。
核心代码
以下提供一套完整的LSTM时间序列预测代码,以股票最高价序列为例,包含数据标准化、训练集形成、网络定义、训练循环和预测接口。
基于LSTM的时间序列预测研究
# coding=utf-8
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#——————————————————导入数据——————————————————————
f = open('dataset_1.csv')
df = pd.read_csv(f) #读入股票数据
data = np.array(df['max']) #获取最高价序列
data = data[::-1]#反转,使数据按照日期先后顺序排列
#以折线图展示data
# plt.figure()
# plt.plot(data)
# plt.show()
normalize_data = (data - np.mean(data)) / np.std(data)#标准化
normalize_data = normalize_data[:, np.newaxis]#增加1个维度
#———————————————————形成训练集—————————————————————
time_step = 20 #时间步
rnn_unit = 10 #hidden layer units
lstm_layers = 2 #每一批次训练多少个样例
batch_size = 60 #输入层维度 #每一批次训练多少个样例
input_size = 1 #输入层维度
output_size = 1 #输出层维度
lr = 0.0006 #学习率
train_x, train_y = [], []#训练集
for i in range(len(normalize_data) - time_step - 1):
x = normalize_data[i:i + time_step]
y = normalize_data[i + 1:i + time_step + 1]
train_x.append(x.tolist())
train_y.append(y.tolist())
# 定义每个X sample的形状(, time_step, input_size)
X = tf.placeholder(tf.float32, [None, time_step, input_size])
# 定义每个Y sample的形状(, time_step, output_size)
Y = tf.placeholder(tf.float32, [None, time_step, output_size])
#——————————————————定义神经网络变量——————————————————
#输入层、输出层权重、偏置
weights = {
'in': tf.Variable(tf.random_normal([input_size, rnn_unit])),
'out': tf.Variable(tf.random_normal([rnn_unit, 1]))
}
print(weights)
biases = {
'in': tf.Variable(tf.constant(0.1, shape=[rnn_unit, ])),
'out': tf.Variable(tf.constant(0.1, shape=[1, ]))
}
print(biases)
#参数:输入网络批次数目
def lstm(batch):#参数:输入网络批次数目
w_in = weights['in']
b_in = biases['in']
print(X)
input = tf.reshape(X, [-1, input_size])#需要将tensor转成2维进行计算,计算后的结果作为隐藏层的输入
print(input)
input_rnn = tf.matmul(input, w_in) + b_in
input_rnn = tf.reshape(input_rnn, [-1, time_step, rnn_unit])#将tensor转成3维,作为lstm cell的输入
cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicLSTMCell(rnn_unit) for i in range(lstm_layers)])
init_state = cell.zero_state(batch, dtype=tf.float32)
output_rnn, final_states = tf.nn.dynamic_rnn(cell, input_rnn, initial_state=init_state, dtype=tf.float32)
output = tf.reshape(output_rnn, [-1, rnn_unit])#作为输出层的输入
w_out = weights['out']
b_out = biases['out']
pred = tf.matmul(output, w_out) + b_out
return pred, final_states
def train_lstm():
global batch_size
with tf.variable_scope("sec_lstm"):
pred, _ = lstm(batch_size)
# 损失函数
loss = tf.reduce_mean(tf.square(tf.reshape(pred, [-1]) - tf.reshape(Y, [-1])))
train_op = tf.train.AdamOptimizer(lr).minimize(loss)
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 重复训练10000次
for i in range(100): # We can increase the number of iterations to gain better result.
step = 0
start = 0
end = start + batch_size
print("i = ",i)
while (end < len(train_x)):
_, loss_ = sess.run([train_op, loss], feed_dict={X: train_x[start:end], Y: train_y[start:end]})
start += batch_size
end = start + batch_size
if step % 100 == 0: #每10步保存一次参数
print("Number of iterations:", i, " loss:", loss_)
print("model_save", saver.save(sess, 'model_save1\\modle.ckpt'))
# I run the code in windows 10,so use 'model_save1\\modle.ckpt'
# if you run it in Linux,please use 'model_save1/modle.ckpt'
step += 1
print("The train has finished")
train_lstm()
input()
prediction()
基于LSTM的时间序列预测研究

