【发布时间】:2021-12-05 12:20:14
【问题描述】:
我正在研究用于时间序列预测的 LSTM 模型。 在我必须重塑模型的输出以计算准确度指数并绘制结果之前,它运行良好。这是我的代码:
train, test = btc.loc[btc.index <= '2020-12-31'], btc.loc[btc.index > '2020-12-31']
scaler = StandardScaler()
scaler = scaler.fit(train)
train, test = scaler.transform(train), scaler.transform(test)
def to_sequences(x, y, seq_size=1):
x_values = []
y_values = []
for i in range(len(x)-seq_size):
x_values.append(x[i:(i+seq_size)])
y_values.append(y[i+seq_size])
return np.array(x_values), np.array(y_values)
seq_size = 30
xtrain, ytrain = to_sequences(train, train, seq_size)
xtest, ytest = to_sequences(test, test, seq_size)
model = Sequential()
model.add(LSTM(128, input_shape=(xtrain.shape[1], xtrain.shape[2])))
model.add(Dropout(rate=0.2))
model.add(RepeatVector(xtrain.shape[1]))
model.add(LSTM(128, return_sequences=True))
model.add(Dropout(rate=0.2))
model.add(TimeDistributed(Dense(xtrain.shape[2])))
model.compile(optimizer='adam', loss='mae')
model.summary()
# fit model
history = model.fit(xtrain, ytrain, epochs=10, batch_size=32, validation_split=0.1, verbose=1)
trainPredict = model.predict(xtrain)
testPredict = model.predict(xtest)
模型预测后,我不知道如何管理它,因为预测的形状是 (1023,30,1) 但我显然需要重塑才能拥有一个二维数组。如果我使用 .reshape(-1,1) 进行整形,我将获得一个具有 (1023, 30) 的二维数组,但我需要一个具有 (1203, 1) 的二维数组,即只有预测值的数组。
【问题讨论】:
-
代码以文本形式提供。