import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM from sklearn.metrics import r2_score,median_absolute_error,mean_absolute_error # 读取数据 data = pd.read_csv(r'C:/Users/Ljimmy/Desktop/yyqc/peijian/销量数据rnn.csv') # 取出特征参数 X = data.iloc[:,2:].values # 数据归一化 scaler = MinMaxScaler(feature_range=(0, 1)) X[:, 0] = scaler.fit_transform(X[:, 0].reshape(-1, 1)).flatten() #X = scaler.fit_transform(X) #scaler.fit(X) #X = scaler.transform(X) # 划分训练集和测试集 train_size = int(len(X) * 0.8) test_size = len(X) - train_size train, test = X[0:train_size, :], X[train_size:len(X), :] # 转换为监督学习问题 def create_dataset(dataset, look_back=1): 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, 0]) return np.array(X), np.array(Y) look_back = 12 X_train, Y_train = create_dataset(train, look_back) #Y_train = train[:, 2:] # 取第三列及以后的数据 X_test, Y_test = create_dataset(test, look_back) #Y_test = test[:, 2:] # 取第三列及以后的数据 # 转换为3D张量 X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)) X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) # 构建LSTM模型 model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1))) model.add(LSTM(units=50)) model.add(Dense(units=1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, Y_train, epochs=5, batch_size=32) #model.fit(X_train, Y_train.reshape(Y_train.shape[0], 1), epochs=10, batch_size=32) # 预测下一个月的销量 last_month_sales = data.tail(12).iloc[:,2:].values #last_month_sales = data.tail(1)[:,2:].values last_month_sales = scaler.transform(last_month_sales) last_month_sales = np.reshape(last_month_sales, (1, look_back, 1)) next_month_sales = model.predict(last_month_sales) next_month_sales = scaler.inverse_transform(next_month_sales) print('Next month sales: %.0f' % next_month_sales[0][0]) # 计算RMSE误差 rmse = np.sqrt(np.mean((next_month_sales - last_month_sales) ** 2)) print('Test RMSE: %.3f' % rmse)IndexError Traceback (most recent call last) Cell In[1], line 36 33 X_test, Y_test = create_dataset(test, look_back) 34 #Y_test = test[:, 2:] # 取第三列及以后的数据 35 # 转换为3D张量 ---> 36 X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)) 37 X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) 38 # 构建LSTM模型 IndexError: tuple index out of range代码修改
时间: 2023-06-12 22:08:25 浏览: 369
在代码中,X_train 和 X_test 的维度为 (样本数量,时间步长),需要将其转换为 (样本数量,时间步长,特征数量) 的形式。因此在创建数据集时,需要将数据 reshape 为 (样本数量,时间步长,1),即每个时间步长只有一个特征。代码如下:
```
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset) - look_back - 1):
a = dataset[i:(i + look_back), :]
X.append(a.reshape(-1, 1))
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
```
在训练和测试数据集转换为3D张量时,需要保证第三个维度为特征数量。因此需要修改如下代码:
```
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
```
修改后的完整代码如下:
```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.metrics import r2_score,median_absolute_error,mean_absolute_error
# 读取数据
data = pd.read_csv(r'C:/Users/Ljimmy/Desktop/yyqc/peijian/销量数据rnn.csv')
# 取出特征参数
X = data.iloc[:,2:].values
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
# 划分训练集和测试集
train_size = int(len(X) * 0.8)
test_size = len(X) - train_size
train, test = X[0:train_size, :], X[train_size:len(X), :]
# 转换为监督学习问题
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset) - look_back - 1):
a = dataset[i:(i + look_back), :]
X.append(a.reshape(-1, 1))
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
look_back = 12
X_train, Y_train = create_dataset(train, look_back)
X_test, Y_test = create_dataset(test, look_back)
# 转换为3D张量
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
# 构建LSTM模型
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(units=50))
model.add(Dense(units=1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=5, batch_size=32)
# 预测下一个月的销量
last_month_sales = data.tail(12).iloc[:,2:].values
last_month_sales = scaler.transform(last_month_sales)
last_month_sales = np.reshape(last_month_sales, (1, look_back, 1))
next_month_sales = model.predict(last_month_sales)
next_month_sales = scaler.inverse_transform(next_month_sales)
print('Next month sales: %.0f' % next_month_sales[0][0])
# 计算RMSE误差
rmse = np.sqrt(np.mean((next_month_sales - last_month_sales) ** 2))
print('Test RMSE: %.3f' % rmse)
```
阅读全文