Keras:如果我用标准化数据训练模型,model.predict() 是否需要标准化数据?

Eri*_*Xue 3 python machine-learning scikit-learn keras tensorflow

使用 Keras 完成模型训练后,我尝试使用 Kerasmodel.predict()来测试新输入的模型。

当我训练模型时,我使用 Scikit Learn 标准化了我的训练数据MinMaxScaler()

使用时是否还需要对数据进行标准化model.predict()?如果是这样,我该怎么做?

Kav*_*veh 6

Yes. You need. Because your model has learned from data with a specific scale, so, it's better to convert your data to the same scale as your model works and then let it predict.

For example, you may use the Scikitlearn library to normalize and standardize the data:

x_scaler = StandardScaler()
x_train = x_scaler.fit_transform(x_train)
x_test = x_scaler.transform(x_test)

# In case you are going to normalize the labels
y_scaler = StandardScaler()
y_train = y_scaler.fit_transform(y_train)
y_test = y_scaler.transform(y_test)
Run Code Online (Sandbox Code Playgroud)

Then, for the prediction you should use the same normalization parameters for the training dataset and then scale reverse to get back to the previous scale with the predicted value like this:

preds = y_scaler.inverse_transform(
         model.predict(x_scaler.transform(pred_input))
         )
Run Code Online (Sandbox Code Playgroud)