数据为一个excle文件,excle文件有一列随时间变化的LS数据,我们使用pytroch里面的lstm模型对LS进行回归预测,用前面50个数据,预测之后10个数据,给出预测评价指标和结果的python代码
时间: 2024-02-21 08:02:14 浏览: 15
以下是使用 PyTorch 中的 LSTM 模型对 Excel 文件中某一列数据进行回归预测的示例代码,以及对预测结果进行评估的指标计算方法:
```python
import pandas as pd
import torch
import torch.nn as nn
from sklearn.metrics import mean_squared_error
# 读取 Excel 文件中的数据
data = pd.read_excel('your_file_name.xlsx', sheet_name='Sheet1')
ls_data = data['LS'].values
# 数据预处理
def prepare_sequence(seq, window_size):
X, y = [], []
for i in range(len(seq)-window_size):
X.append(seq[i:i+window_size])
y.append(seq[i+window_size])
return torch.tensor(X).float(), torch.tensor(y).float()
# 划分训练集和测试集
train_data = ls_data[:int(0.8*len(ls_data))]
test_data = ls_data[int(0.8*len(ls_data)):]
# 设置超参数
input_size = 1
hidden_size = 64
num_layers = 2
output_size = 1
lr = 0.01
num_epochs = 100
# 构建 LSTM 模型
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size):
super(LSTM, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
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):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
return out
# 实例化 LSTM 模型和损失函数
model = LSTM(input_size, hidden_size, num_layers, output_size).to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
# 训练 LSTM 模型
for epoch in range(num_epochs):
model.train()
train_X, train_y = prepare_sequence(train_data, 50)
train_X, train_y = train_X.to(device), train_y.to(device)
optimizer.zero_grad()
outputs = model(train_X)
loss = criterion(outputs, train_y)
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, loss.item()))
# 测试 LSTM 模型
model.eval()
test_X, test_y = prepare_sequence(test_data, 50)
test_X, test_y = test_X.to(device), test_y.to(device)
with torch.no_grad():
outputs = model(test_X)
test_loss = criterion(outputs, test_y)
print('Test Loss: {:.4f}'.format(test_loss.item()))
# 计算预测结果的评估指标
y_pred = outputs.cpu().numpy().flatten()
y_true = test_y.cpu().numpy().flatten()
mse = mean_squared_error(y_true, y_pred)
rmse = mse ** 0.5
print('MSE: {:.4f}, RMSE: {:.4f}'.format(mse, rmse))
```
其中 `prepare_sequence` 函数用于将原始数据序列转换为 LSTM 模型的输入和输出序列,`input_size`、`hidden_size`、`num_layers`、`output_size` 分别表示 LSTM 模型的输入维度、隐藏状态维度、层数和输出维度,`lr` 表示学习率,`num_epochs` 表示训练轮数。在训练过程中,我们使用均方误差(MSE)作为损失函数,使用 Adam 优化器进行参数更新。在测试过程中,我们计算预测结果的 MSE 和 RMSE 作为评估指标。