python操作resultym.csv数据表(有Date(YYYY/MM)、TotalPrice两列数据),数据表第一行为表头信息,数据表中前27行都有数据,以此为基础,python调用resultym.csv表进行操作:循环调用以resultym.csv为数据集构建的pytorch lstm预测模型(模型实现过程:先读取shuju.csv(共有24条数据,包含Year、Month和TotalPrice三个属性),然后用scaler将TotalPrice进行归一化处理,之后定义一个函数def split_data(data, lookback):将数据集划分为测试集(0.2)和训练集(0.8),data_raw = data.to_numpy(),lookback = 4,然后再将划分完成后的测试集和训练集转换为PyTorch张量,然后定义超参数,定义算法模型model=LSTM()、损失函数和优化器(Adam)然后训练模型),该模型能够根据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 22:44:58 浏览: 150
LSTM时间序列预测(Python完整源码和数据)
5星 · 资源好评率100%
首先,我们需要导入相关的库和模块:
```python
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
```
然后,我们需要读取resultym.csv文件,获取前27行数据,以及读取shuju.csv文件,获取24条数据:
```python
# 读取resultym.csv文件
data = pd.read_csv('resultym.csv', header=0, usecols=[0, 1])
data = data.iloc[:27]
# 读取shuju.csv文件
data_raw = pd.read_csv('shuju.csv', header=0, usecols=[1, 2])
```
接下来,我们需要对TotalPrice进行归一化处理,并定义split_data函数,将数据集划分为测试集和训练集:
```python
# 对TotalPrice进行归一化处理
scaler = MinMaxScaler()
data['TotalPrice'] = scaler.fit_transform(data['TotalPrice'].values.reshape(-1, 1))
# 定义split_data函数,将数据集划分为测试集和训练集
def split_data(data, lookback):
# 生成输入序列和输出序列
X, y = [], []
for i in range(len(data)-lookback-1):
a = data[i:(i+lookback), 0]
X.append(a)
y.append(data[i + lookback, 0])
return np.array(X), np.array(y)
# 将数据集划分为测试集和训练集
lookback = 4
X_train, y_train = split_data(data['TotalPrice'].values.reshape(-1, 1), lookback)
X_test, y_test = split_data(data_raw['TotalPrice'].values.reshape(-1, 1), lookback)
# 将划分完成后的测试集和训练集转换为PyTorch张量
X_train = torch.from_numpy(X_train).type(torch.Tensor)
X_test = torch.from_numpy(X_test).type(torch.Tensor)
y_train = torch.from_numpy(y_train).type(torch.Tensor)
y_test = torch.from_numpy(y_test).type(torch.Tensor)
# 对张量进行reshape操作
X_train = Variable(X_train.view(-1, 1, lookback))
X_test = Variable(X_test.view(-1, 1, lookback))
y_train = Variable(y_train)
y_test = Variable(y_test)
```
然后,我们需要定义超参数、算法模型、损失函数和优化器:
```python
# 定义超参数
input_dim = 1
hidden_dim = 2
num_layers = 1
output_dim = 1
num_epochs = 1000
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 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_dim))
c0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_dim))
out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:, -1, :])
return out
model = LSTM(input_dim=input_dim, hidden_dim=hidden_dim, output_dim=output_dim, num_layers=num_layers)
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
```
接下来,我们需要训练模型,并循环调用模型进行预测,将预测出的值插入到resultym.csv表格的最后一行:
```python
# 训练模型
for epoch in range(num_epochs):
outputs = model(X_train)
optimizer.zero_grad()
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
# 循环调用模型进行预测
for i in range(24):
x = X_test[i].unsqueeze(0)
y_test_pred = model(x).item()
y_test = torch.cat([y_test, torch.Tensor([y_test_pred])])
data = data.append({'Date': '2023/'+str(int(data_raw.iloc[i, 1])+1).zfill(2), 'TotalPrice': 2222222.5}, ignore_index=True)
# 将预测出的值插入到resultym.csv表格的最后一行
for i in range(26, 51):
x = data['TotalPrice'][i-lookback:i].values.reshape(-1, 1)
x = scaler.transform(x)
x = torch.from_numpy(x).type(torch.Tensor)
x = Variable(x.view(-1, 1, lookback))
y_pred = model(x).item()
y_pred = scaler.inverse_transform(np.array([[y_pred]]))
data = data.append({'Date': '2023/'+str(int(data_raw.iloc[i-lookback, 1])+1).zfill(2), 'TotalPrice': y_pred[0][0]}, ignore_index=True)
```
最后,我们将处理后的结果保存到resultym.csv文件中:
```python
# 将处理后的结果保存到resultym.csv文件中
data.to_csv('resultym.csv', index=False)
```
阅读全文