python操作resultym.csv数据表(有Date(YYYY/MM)、TotalPrice两列数据),数据表第一行为表头信息,数据表中前27行都有数据,以此为基础,python调用resultym.csv表进行操作:循环调用以resultym.csv为数据集构建的pytorch lstm预测模型(模型实现过程:读取csv表,然后将TotalPrice归一化,接着按照0.8划分训练集和测试集,lookback=4,然后将划分好的数据转为PyTorch张量,之后定义超参数和算法模型、优化器,最后训练模型),该模型能够根据Date值来预测TotalPrice值,然后将第一次预测出的y_test_pred赋值给B26、将第二次预测出的值赋给B27、将第三次预测出的值赋给B28,一直循环直到求出B50的数值。每预测出一个值就在表的最后一行插入一组数据,插入的数据为:Date插入的值按照前面的年月往下延(即按照2023/03、2023/04、2023/05········2025/01的顺序),TotalPrice插入的值定义为2222222.5。直到求出第50行的数值,脚本停止运行。
时间: 2023-12-06 12:44:50 浏览: 139
以下是大致的代码实现思路,需要根据具体数据集和模型进行调整:
```python
import pandas as pd
import numpy as np
import torch
import torch.nn as nn
from sklearn.preprocessing import MinMaxScaler
# 读取数据集
data = pd.read_csv("resultym.csv")
data['Date'] = pd.to_datetime(data['Date'], format='%Y/%m')
data.set_index('Date', inplace=True)
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
data_scaled = scaler.fit_transform(data)
# 划分训练集和测试集
train_size = int(len(data_scaled) * 0.8)
test_size = len(data_scaled) - train_size
train_data = data_scaled[0:train_size, :]
test_data = data_scaled[train_size:len(data_scaled), :]
# 将数据转为 PyTorch 张量
def create_dataset(dataset, look_back=4):
X, y = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), :]
X.append(a)
y.append(dataset[i + look_back, 1])
return torch.tensor(X), torch.tensor(y)
look_back = 4
X_train, y_train = create_dataset(train_data, look_back)
X_test, y_test = create_dataset(test_data, look_back)
# 定义超参数和模型
input_dim = X_train.shape[-1]
hidden_dim = 32
num_layers = 1
output_dim = 1
num_epochs = 100
learning_rate = 0.01
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
super(LSTM, self).__init__()
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_()
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_()
out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:, -1, :])
return out
model = LSTM(input_dim, hidden_dim, num_layers, output_dim)
# 定义优化器和损失函数
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
outputs = model(X_train.float())
optimizer.zero_grad()
loss = criterion(outputs.squeeze(), y_train.float())
loss.backward()
optimizer.step()
if epoch % 10 == 0:
print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
# 预测测试集数据
model.eval()
X_test = X_test.float()
y_test_pred = model(X_test).squeeze().detach().numpy()
y_test_pred = scaler.inverse_transform(np.concatenate((np.zeros((look_back, 1)), y_test_pred.reshape(-1, 1))))
# 将预测结果插入数据表
for i in range(len(y_test_pred)):
date = pd.to_datetime(data.index[train_size+i+look_back], format='%Y-%m-%d')
data.loc[date] = [2222222.5] * len(data.columns)
data.iloc[-1, 1] = y_test_pred[i, 0]
if i == 23:
data.to_csv("resultym_predicted.csv", index=True) # 保存预测结果
if i == 46:
break
```
注意,由于我没有完整的数据集和模型,以上代码可能还需要进行一些修改和调整。
阅读全文