TensorFlow Normalization vs Scikit-learn Normalization

Viewed 49

I've been working through a machine learning course, and one of the extra circular assignments at the end of the Regression lesson is to

Import the Boston pricing dataset from TensorFlow tf.keras.datasets and model it.

During the course I learned that normalizing the dataset is beneficial to training the model so I wanted to give it a try on the Boston dataset. The example the instructor gave on normalization used the sklearn library, but during my search I found TensorFlow also has a normalization utility, tf.keras.utils.normalize.

The TensorFlow solution is so much simpler, which made we wonder why the instructor didn't use that over the sklearn method. Which brings me to my question:

Is there a particular reason/use case when I should choose one method of normalization over the other, or is it just a matter of preference?

TensorFlow Normalization that I am using in my code:

X_train_normalized = tf.keras.utils.normalize(X_train)
X_test_normalized = tf.keras.utils.normalize(X_test)

sklearn Normalization as demonstrated in the course:

# Create column transformer (this will help us normalize/preprocess our data)
ct = make_column_transformer(
    (MinMaxScaler(), ["age", "bmi", "children"]), # get all values between 0 and 1
    (OneHotEncoder(handle_unknown="ignore"), ["sex", "smoker", "region"])
)

# Create X & y
X = insurance.drop("charges", axis=1)
y = insurance["charges"]

# Build our train and test sets (use random state to ensure same split as before)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Fit column transformer on the training data only (doing so on test data would result in data leakage)
ct.fit(X_train)

# Transform training and test data with normalization (MinMaxScalar) and one hot encoding (OneHotEncoder)
X_train_normal = ct.transform(X_train)
X_test_normal = ct.transform(X_test)
1 Answers

Note, even you could define Normalization layer and adapt it on training data before training model and then implement the layer with calculated mean, variance in your model structure.

norm_layer = tf.keras.layers.Normalization(axis=-1)
norm_layer.adapt(X_train)

so i think it would be just depend on the case you are working on, specially if your dataset just requires normalization. and as you know the neural networks as same as linear models would have best performance on normalized dataset.

Related