lstm预测模型代码
时间: 2023-09-29 11:09:16 浏览: 119
下面是用于LSTM预测模型的代码:
```python
import matplotlib.pyplot as plt
from pandas import read_csv, DataFrame, concat
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from numpy import concatenate
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from math import sqrt
import tensorflow as tf
# 设置随机数种子
tf.random.set_seed(2)
# 导入数据集
qy_data = read_csv(r'C:\Users\HUAWEI\Desktop\abc.csv', parse_dates=['num'], index_col='num')
qy_data.index.name = 'num' # 选定索引列
# 数据预处理
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(qy_data)
# 划分训练集和测试集
train_size = int(len(scaled_data) * 0.8)
test_size = len(scaled_data) - train_size
train_data = scaled_data[:train_size]
test_data = scaled_data[train_size:]
# 创建训练集和测试集的特征和标签
def create_dataset(dataset, time_steps=1):
dataX, dataY = [], []
for i in range(len(dataset) - time_steps):
a = dataset[i:(i + time_steps), :]
dataX.append(a)
dataY.append(dataset[i + time_steps, :])
return numpy.array(dataX), numpy.array(dataY)
time_steps = 1
train_X, train_y = create_dataset(train_data, time_steps)
test_X, test_y = create_dataset(test_data, time_steps)
# 初始化LSTM模型
model = Sequential()
model.add(LSTM(27, input_shape=(train_X.shape
阅读全文