[FIXED] Keras: Does model.predict() require normalized data if I train the model with normalized data?

Issue

After completing model training using Keras I am trying to use Keras’ model.predict() in order to test the model on novel inputs.

When I trained the model, I normalized my training data with Scikit Learn’s MinMaxScaler().

Do I need to normalize the data as well when using model.predict()? If so, how do I do it?

Solution

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, 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)

y_scaler = StandardScaler()
y_train = y_scaler.fit_transform(y_train)
y_test = y_scaler.transform(y_test)

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))
         )

Answered By – Kaveh

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published