我关于Keras回归LSTM的所有示例均使用一列数据进行未来预测。但是,如果我想包括更多列,我该如何重塑数据以包括在回归模型中。 在下面提到的示例中,功能使用df.Close列来预测未来价格。可以说我想使用数据框的所有四列作为模型的输入字段,以供将来预测 任何帮助将受到广泛赞赏。我已经搜索了这个答案,但是没有找到这个非常明显的答案。
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
df = pd.read_csv("https://raw.githubusercontent.com/mwitiderrick/stockprice/master/NSE-TATAGLOBAL.csv")
##Custom function to reshape the data
def datashape(batch_size, scaled_df):
X_train_def = []
y_train_def = []
for i in range(batch_size, len(scaled_df)):
X_train_def.append(scaled_df[i-batch_size:i, 0])
y_train_def.append(scaled_df[i, 0])
X_train_def, y_train_def = np.array(X_train_def), np.array(y_train_def)
X_train_def = np.reshape(X_train_def, (X_train_def.shape[0], X_train_def.shape[1], 1))
return X_train_def, y_train_def
close = df['Close']
close = close.values.reshape(len(close), 1)
#normalize data
scaler = MinMaxScaler(feature_range=(0,1))
close = scaler.fit_transform(close)
#split data into train and test
train_size = int(len(close)* 0.7)
test_size = len(close) - train_size
close_train, close_test= close[0:train_size, :], close[train_size:len(close), :]
### This is the chunk of data goes in machine
batch_size = 60
trainX, trainY = datashape(batch_size,close_train)
testX, testY = datashape(batch_size,close_test)
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras import optimizers
model = Sequential()
model.add(LSTM(units = 64, return_sequences = True, input_shape=(batch_size, 1)))
model.add(LSTM(units = 64, return_sequences = True))
model.add(LSTM(units = 64))
model.add(Dense(units = 1))
optimizer = optimizers.RMSprop(0.001)
model.compile(optimizer = optimizer,
loss = 'mean_squared_error',
metrics=['mean_absolute_error', 'mean_squared_error'])
model.fit(trainX, trainY, epochs = 5, batch_size = 32)