运行超参数调整后,我有以下代码来绘制预测和实际值。
def cv(n_splits, LSTM_unit, lr, epoch, batch_size, saving_dir,
X, Y, seed):
"""
This is the function to do cross validation from the hyperparameter tuning
that we already have
Args:
:n_split: Number of cv split(int)
:LSTM_unit: Number of unit in LSTM model(int)
:lr: Learning Rate for the model
:epoch: Epoch for the model(int)
:batch_size: Batch size for the model
:saving_dir: Directory in which the model is saved(str)
:X: Feature set (np.ndarray)
:Y: Target label (np.ndarray)
:seed: seed for CV random state(int)
"""
model_name = " ".join([str(LSTM_unit), str(lr), str(epoch), str(batch_size)])
kfold = KFold(n_splits=n_splits, shuffle=True, random_state=seed)
cvscores = []
hist_pred = []
hist_actual = []
# Load the model model
model = load_model(saving_dir+"{}.h5".format(model_name))
for train, test in kfold.split(X, Y):
# evaluate the model
X[test] = np.array(X[test])
X[test] = X[test].reshape((X[test].shape[0], 1, X[test].shape[-1]))
predictions = model.predict(X[test])
# print(predictions)
hist_pred.extend(predictions)
hist_actual.extend(Y[test])
scores = np.sqrt(np.mean((predictions - Y[test]**2)))
cvscores.append(scores)
print('CV-RMSE = {} with {} standard deviation'.format(np.mean(cvscores),
np.std(cvscores)))
hist_pred = label_scaler.inverse_transform(hist_pred)
hist_actual = label_scaler.inverse_transform(hist_actual)
#Plot/Create the data for the graph
plt.figure(figsize=(10,5))
plt.plot(hist_pred, color = 'green', label = 'Predictions')
plt.plot(hist_actual, color = 'red', label = 'Real Value')
plt.title('')
plt.ylabel('')
plt.legend()
plt.show()
代码运行良好。我想要做的是,使用上面的代码(没有交叉验证)进行样本预测。我的意思是,想象一个时间序列数据集,它有特征变量但没有标签。我想在没有交叉验证的情况下使用已经建立的模型来预测这些标签。既然是时间序列未来预测,用CV没有意义。
“举个例子,假设我想预测本月底将发布的通货膨胀。我现在拥有所有特征变量来预测它。”
如何修改上面的代码?任何帮助将不胜感激。
谢谢。