以下是Python中LSTM回归神经网络时间序列预测的完整攻略,包括两个示例。
LSTM回归神经网络时间序预测的基本步骤如下:
import numpy as
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
准备时间序列数据,将其转换为适合LSTM模的格式。
class TimeSeriesDataset(Dataset):
def __init__(self, data, seq_len):
self.data = data
self.seq_len = seq_len
def __len__(self):
return len(self.data) - self.seq_len
def __getitem__(self, idx):
x = self.data[idx:idx+self.seq_len]
y = self.data[idx+self.seq_len]
return x, y
# 加载数据
data = pd.read_csv('data.csv', header=None)
data = data.values.astype('float32')
# 划分训练集和测试集
train_size int(len(data) * 0.8)
train_data = data[:train_size]
test_data = data[train_size:]
# 标准化数据
mean = train_data.mean(axis=0)
std = train_data.std(axis=0)
train_data = (train_data - mean) / std
test_data = (test_data - mean) / std
# 创建数据集
seq_len = 10
train_dataset = TimeSeriesDataset(train_data, seq_len)
test_dataset = TimeSeriesDataset(test_data, seq_len)
# 创建数据加载器
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
定义LSTM模型。
class LSTM(nn.Module):
def __init__(self, input_size,_size, num_layers, output_size):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
out, _ = self.lstm(x)
out = out[:, -1, :]
out = self.fc(out)
return out
训练LSTM模型。
# 定义模型
input_size = 1
hidden_size = 32
num_layers 2
output_size = 1
model = LSTM(input_size, hidden_size, num_layers, output_size)
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# 训练模型
num_epochs = 100
for epoch in range(num_epochs):
for i, (x, y) in enumerate(train_loader):
# 前向传播
x = x.unsqueeze(-1)
y_pred = model(x)
# 计算损失
loss = criterion(y_pred, y)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 打印损失
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
测试LSTM模型。
# 测试模型
model.eval()
with torch.no_grad():
y_pred = []
for x, y in test_loader:
x = x.unsqueeze(-1)
y_pred.append(model(x).squeeze().numpy())
y_pred = np.concatenate(y_pred)
# 反标准化数据
y_pred = y_pred * std[-1] + mean[-1]
y_true = test_data[seq_len:, -1] * std[-1] + mean[-1]
# 绘制预测结果
plt.plot(y_true, label='True')
plt.plot(y_pred, label='Predicted')
plt.legend()
plt.show()
以上是Python中LSTM回归神经网络时间序列预测的完整攻略,通过以上步骤和示例,我们可以轻松地使用LSTM模型进行时间序列预测。
以下是使用LSTM模型预测股票价格的示例代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# 加载数据
data = pd.read_csv('stock.csv')
data = data['Close'].values.astype('float32')
# 划分训练集和测试集
train_size = int(len(data) * 0.8)
train_data = data[:train_size]
test_data = data[train_size:]
# 标准化数据
mean = train_data.mean()
std = train_data.std()
train_data = (train_data - mean) / std
test_data = (test_data - mean) / std
# 创建数据集
seq_len = 10
train_dataset = TimeSeriesDataset(train_data, seq_len)
test_dataset = TimeSeriesDataset(test_data, seq_len)
# 创建数据加载器
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 定义模型
input_size = 1
hidden_size = 32
num_layers = 2
output_size = 1
model = LSTM(input_size, hidden_size, num_layers, output_size)
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# 训练模型
num_epochs = 100
for epoch in range(num_epochs):
for i, (x, y) in enumerate(train_loader):
# 前向传播
x = x.unsqueeze(-1)
y_pred = model(x)
# 计算损失
loss = criterion(y_pred, y)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 打印损失
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
# 测试模型
model.eval()
with torch.no_grad():
y_pred = []
for x, y in test_loader:
x = x.unsqueeze(-1)
y_pred.append(model(x).squeeze().numpy())
y_pred = np.concatenate(y_pred)
# 反标准化数据
y_pred = y_pred * std + mean
y_true = test_data[seq_len:] * std + mean
# 绘制预测结果
plt.plot(y, label='True')
plt.plot(y_pred, label='Predicted')
plt.legend()
plt.show()
上面的代码使用LSTM模型预测股票价格。
以下使用LSTM模型预测气温的示例代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# 加载数据
data = pd.read_csv('temperature.csv')
data = data['Temperature'].values.astype('float32')
# 划分训练集和测试集
train_size = int(len(data) * 0.8)
train_data = data[:train_size]
test_data = data[train_size:]
# 标准化数据
mean = train_data.mean()
std = train_data.std()
train_data = (train_data - mean) / std
test_data = (test_data - mean) / std
# 创建数据集
seq_len = 10
train_dataset = TimeSeriesDataset(train_data, seq_len)
test_dataset = TimeSeriesDataset(test_data, seq_len)
# 创建数据加载器
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 定义模型
input_size = 1
hidden_size = 32
num_layers = 2
output_size = 1
model LSTM(input_size, hidden_size, num_layers, output_size)
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# 训练模型
num_epochs =
for epoch in range(num_epochs):
for i, (x, y) in enumerate(train_loader):
# 前向传播
x = x.unsqueeze(-1)
y_pred = model(x)
# 计算损失
loss = criterion(y_pred, y)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 打印损失
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
# 测试模型
model.eval()
with torch.no_grad():
y_pred = []
for x, y in test_loader:
x = x.unsqueeze(-1)
y_pred.append(model(x).squeeze().numpy())
y_pred = np.concatenate(y_pred)
# 反标准化数据
y_pred = y_pred * std + mean
y_true = test_data[seq_len:] * std + mean
# 绘制预测结果
plt.plot(y_true, label='True')
plt.plot(y_pred label='Predicted')
plt.legend()
plt.show()
上面的代码使用LSTM模型预测气温。
本文链接:http://task.lmcjl.com/news/16553.html