Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: Using Predict with a Model Trained with Normalized Data?

Tags:

python

keras

I'm creating a deep neural network in Keras to perform an NN regression using tabular data. Best practice is to normalize the inputs and output series. I'd also like to use the predict function to provide estimates of the model's output for various sets of inputs. If the training data was normalized, I assume I'll need to also normalize the predict data set using the same scaling parameters. What's the best way to do this? Is there a way to automatically normalize the data within the model?

like image 772
Steve Maughan Avatar asked Jun 21 '26 08:06

Steve Maughan


1 Answers

I typically like to use sklearn for this, and it does save the parameters and allows you to "inverse transform" back to the original values. For predictions you would send them through the inverse_transform function to get their real predicted values.

Here is a working example for you to reference. The parameters of the scalers can be easily adjusted.

from sklearn.preprocessing import MinMaxScaler, StandardScaler
import numpy as np

example = np.array([0., 1., 1., 0., 2., 3., 4., 4., 5.]).reshape(-1, 1)

# MinMax Scaling Example
scaler = MinMaxScaler(feature_range=(0.01, 0.99))
min_max_scaled = scaler.fit_transform(example)
min_max_orig = scaler.inverse_transform(min_max_scaled)

# Normalizing Example  (mean 0, std 1)
norm = StandardScaler()
normalized = norm.fit_transform(example)
normalized_orig = norm.inverse_transform(normalized)
like image 121
Chris Farr Avatar answered Jun 23 '26 22:06

Chris Farr



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!