在使用Keras完成模型训练后,我尝试使用Keras的model.predict()
来测试模型对新输入的预测效果。
在训练模型时,我使用Scikit Learn的MinMaxScaler()
对训练数据进行了归一化处理。
在使用model.predict()
时,我也需要对数据进行归一化吗?如果需要,应该如何操作?
回答:
是的,需要。因为您的模型已经从特定尺度的数据中学习,所以最好将您的数据转换为与模型工作时相同的尺度,然后再进行预测。
例如,您可以使用Scikitlearn库来归一化和标准化数据:
x_scaler = StandardScaler()x_train = x_scaler.fit_transform(x_train)x_test = x_scaler.transform(x_test)# 如果您打算对标签进行归一化y_scaler = StandardScaler()y_train = y_scaler.fit_transform(y_train)y_test = y_scaler.transform(y_test)
然后,对于预测,您应该使用与训练数据集相同的归一化参数,然后反向缩放以将预测值恢复到之前的尺度,像这样:
preds = y_scaler.inverse_transform( model.predict(x_scaler.transform(pred_input)) )