Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 585
Eric Xue Avatar asked Oct 14 '25 14:10

Eric Xue


1 Answers

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)

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))
         )
like image 196
Kaveh Avatar answered Oct 17 '25 05:10

Kaveh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!