数据为一个excle文件,excle文件有一列随时间变化的LS数据,我们使用pytroch里面的lstm模型对LS进行回归预测,用前面50个数据,预测之后10个数据,给出预测评价指标和图片的python代码
时间: 2024-02-22 14:55:32 浏览: 81
基于Relieff算法(数据特征选择算法)的回归预测,多输入单输出模型 评价指标包括:R2、MAE、MSE、RMSE等,代码
好的,我可以为您提供一份python代码来解决您的问题。
首先,需要安装以下库:
- pandas
- numpy
- matplotlib
- torch
然后,将您的数据导入到pandas DataFrame中:
```python
import pandas as pd
df = pd.read_excel('data.xlsx')
```
接下来,我们需要对数据进行归一化处理:
```python
import numpy as np
data = df['LS'].values.reshape(-1, 1)
scaler = MinMaxScaler(feature_range=(-1, 1))
data = scaler.fit_transform(data)
```
然后,将数据分成训练集和测试集:
```python
train_size = int(len(data) * 0.8)
test_size = len(data) - train_size
train_data, test_data = data[0:train_size,:], data[train_size:len(data),:]
```
接下来,我们需要创建一个函数来将数据转换为适合LSTM模型的数据格式:
```python
def create_dataset(dataset, look_back=50, look_forward=10):
data_X, data_Y = [], []
for i in range(len(dataset)-look_back-look_forward):
a = dataset[i:(i+look_back), 0]
data_X.append(a)
data_Y.append(dataset[i + look_back:i + look_back + look_forward, 0])
return np.array(data_X), np.array(data_Y)
```
然后,我们使用该函数来创建我们的训练集和测试集:
```python
train_X, train_Y = create_dataset(train_data)
test_X, test_Y = create_dataset(test_data)
```
接下来,我们需要将数据转换为PyTorch张量:
```python
import torch
train_X = torch.from_numpy(train_X).float()
train_Y = torch.from_numpy(train_Y).float()
test_X = torch.from_numpy(test_X).float()
test_Y = torch.from_numpy(test_Y).float()
```
接下来,我们定义我们的LSTM模型:
```python
class LSTM(nn.Module):
def __init__(self, input_size=1, hidden_size=100, output_size=10, num_layers=1):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
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).requires_grad_()
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).requires_grad_()
out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:, -1, :])
return out
```
接下来,我们实例化我们的模型:
```python
model = LSTM()
```
然后,我们定义我们的损失函数和优化器:
```python
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
```
接下来,我们进行模型训练:
```python
num_epochs = 100
for epoch in range(num_epochs):
outputs = model(train_X)
loss = criterion(outputs, train_Y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 10 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
```
最后,我们使用训练好的模型来进行预测并绘制预测结果图表:
```python
import matplotlib.pyplot as plt
model.eval()
train_predict = model(train_X)
test_predict = model(test_X)
train_predict = scaler.inverse_transform(train_predict.detach().numpy())
train_Y = scaler.inverse_transform(train_Y.detach().numpy())
test_predict = scaler.inverse_transform(test_predict.detach().numpy())
test_Y = scaler.inverse_transform(test_Y.detach().numpy())
plt.plot(df['LS'].values, label='True Data')
plt.plot(range(50, 50+len(train_predict)), train_predict, label='Training Prediction')
plt.plot(range(50+len(train_predict)+10, len(df['LS'].values)), test_predict, label='Test Prediction')
plt.legend()
plt.show()
```
至此,我们的问题得到了解决,预测评价指标包括均方根误差(RMSE)和平均绝对误差(MAE),您可以使用以下代码来计算这些指标:
```python
from sklearn.metrics import mean_squared_error, mean_absolute_error
train_rmse = np.sqrt(mean_squared_error(train_Y, train_predict))
train_mae = mean_absolute_error(train_Y, train_predict)
test_rmse = np.sqrt(mean_squared_error(test_Y, test_predict))
test_mae = mean_absolute_error(test_Y, test_predict)
print('Train RMSE: {:.2f}'.format(train_rmse))
print('Train MAE: {:.2f}'.format(train_mae))
print('Test RMSE: {:.2f}'.format(test_rmse))
print('Test MAE: {:.2f}'.format(test_mae))
```
阅读全文