这是一个基于PyTorch的简单多特征LSTM时间序列预测代码示例。假设我们有一个时间序列数据集,它有两个特征(feature1和feature2)。
python
复制代码
import torch
import torch.nn as nn
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
# 1. 数据预处理
# 假设我们有如下数据
data = np.array([
[1, 2],
[2, 3],
[3, 4],
[4, 5],
[5, 6]
])
# 添加偏移量以提高预测的准确性
data = data[:, :-1]
target = data[:, -1]
# 数据归一化
scaler = MinMaxScaler()
data = scaler.fit_transform(data)
target = scaler.transform(target.reshape(-1, 1))
# 划分训练集和测试集 &