假设CSV文件名为temperature_data.csv
,其前五行和标题如下:
这里,我们只使用Temperature
列进行单步预测。以下是整合的代码示例:
import pandas as pd
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
# 加载数据
data = pd.read_csv('temperature_data.csv')
# 选择Temperature列
temperatures = data['Temperature'].values.reshape(-1, 1)
# 数据归一化
scaler = MinMaxScaler()
temperatures = scaler.fit_transform(temperatures)
# 创建数据集函数
def create_dataset(data, time_step=1):
dataX, dataY = [], []
for i in range(len(data)-time_step-1):
a = data[i:(i+time_step), 0]
dataX.append(a)
dataY.append(data[i + time_step, 0])
return np.array(dataX), np.array(dataY)
# 定义时间步长
time_step = 5
X, y = create_dataset(temperatures, time_step)
# 重塑输入数据为[samples, time_step, features]
X = X.reshape(X.shape[0], X.shape[1], 1)
# 转换为PyTorch张量
X = torch.tensor(X, dtype=torch.float32)
y = torch.tensor(y, dtype=torch.float32)
# 定义LSTM模型
class LSTMModel(nn.Module):
def __init__(self):
super(LSTMModel, self).__init__()
self.lstm = nn.LSTM(input_size=1, hidden_size=50, num_layers=1, batch_first=True)
self.fc = nn.Linear(50, 1)
def forward(self, x):
output, (hn, cn) = self.lstm(x)
y_pred = self.fc(output[:, -1, :])
return y_pred
# 初始化模型、损失函数和优化器
model = LSTMModel()
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练模型
epochs = 100
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
outputs = model(X)
loss = criterion(outputs, y.unsqueeze(2))
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item()}')
# 测试模型
model.eval()
with torch.no_grad():
predicted = model(X)
predicted = predicted.numpy().squeeze()
# 反归一化
predicted = scaler.inverse_transform(predicted.reshape(-1, 1))
# 绘制实际值和预测值
plt.figure(figsize=(10, 6))
plt.plot(y.numpy().squeeze(), label='Actual')
plt.plot(predicted, label='Predicted')
plt.title('Temperature Prediction')
plt.xlabel('Time Step')
plt.ylabel('Temperature')
plt.legend()
plt.show()
请注意,这段代码假设你已经有了一个名为temperature_data.csv
的CSV文件,并且该文件位于你的工作目录中。此外,代码中的time_step
变量可以根据需要调整,以改变模型预测的时间范围。这个例子中的模型是一个简单的LSTM网络,它可以根据前time_step
个时间步的气温数据来预测下一个时间步的气温。