python实现lstm多步预测
时间: 2023-06-20 16:10:36 浏览: 101
A-LSTM 遗传算法优化的lstm 预测代码 python实现.zip
LSTM(Long Short-Term Memory)是一种常用的循环神经网络,可以用于时间序列数据的建模和预测。多步预测是指预测多个时间步长的未来值。下面是Python实现LSTM多步预测的示例代码。
首先,导入所需的库:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
```
接着,读取并处理数据。假设我们有一个时间序列数据集,包含10年每月销售额的信息。
```python
# 读取数据
data = pd.read_csv('sales.csv', header=None)
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data)
```
然后,将数据集划分为训练集和测试集。
```python
# 划分训练集和测试集
train_size = int(len(data) * 0.67)
test_size = len(data) - train_size
train, test = data[0:train_size,:], data[train_size:len(data),:]
```
接下来,将训练集和测试集转换为可以用于LSTM模型训练的输入和输出。
```python
# 将数据转换为可以用于LSTM模型训练的输入和输出
def create_dataset(dataset, look_back=1, look_forward=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-look_forward+1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[(i+look_back):(i+look_back+look_forward), 0])
return np.array(dataX), np.array(dataY)
# 设置时间步长
look_back = 12
look_forward = 3
# 创建训练数据集
trainX, trainY = create_dataset(train, look_back, look_forward)
# 创建测试数据集
testX, testY = create_dataset(test, look_back, look_forward)
```
然后,定义LSTM模型并进行训练。
```python
# 定义LSTM模型
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(look_forward))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX.reshape((trainX.shape[0], 1, trainX.shape[1])), trainY, epochs=100, batch_size=1, verbose=2)
```
最后,使用训练好的模型进行多步预测。
```python
# 使用训练好的模型进行多步预测
trainPredict = model.predict(trainX.reshape((trainX.shape[0], 1, trainX.shape[1])))
testPredict = model.predict(testX.reshape((testX.shape[0], 1, testX.shape[1])))
# 反归一化
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(trainY)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(testY)
```
完整代码如下:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
# 读取数据
data = pd.read_csv('sales.csv', header=None)
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data)
# 划分训练集和测试集
train_size = int(len(data) * 0.67)
test_size = len(data) - train_size
train, test = data[0:train_size,:], data[train_size:len(data),:]
# 将数据转换为可以用于LSTM模型训练的输入和输出
def create_dataset(dataset, look_back=1, look_forward=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-look_forward+1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[(i+look_back):(i+look_back+look_forward), 0])
return np.array(dataX), np.array(dataY)
# 设置时间步长
look_back = 12
look_forward = 3
# 创建训练数据集
trainX, trainY = create_dataset(train, look_back, look_forward)
# 创建测试数据集
testX, testY = create_dataset(test, look_back, look_forward)
# 定义LSTM模型
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(look_forward))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX.reshape((trainX.shape[0], 1, trainX.shape[1])), trainY, epochs=100, batch_size=1, verbose=2)
# 使用训练好的模型进行多步预测
trainPredict = model.predict(trainX.reshape((trainX.shape[0], 1, trainX.shape[1])))
testPredict = model.predict(testX.reshape((testX.shape[0], 1, testX.shape[1])))
# 反归一化
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(trainY)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(testY)
```
阅读全文