# difference between scaling/normalizing data at a specific step

I am using the MinMaxScaler normalization method, however I have seen various ways that this can be done, I want to know if there is any actual difference between the following:

1. Standardizing/Normalizing the data before splitting the data into train and test

Code 1

scaler = MinMaxScaler() #Normalization
#Transform X and Y values with scaler
x = scaler.fit_transform(x)
y = y.reshape(-1,1)
y = scaler.fit_transform(y)

# Split Data in train and validation
x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.25)


2. Standardizing/Normalizing the data after splitting the data into train and test and then scaling on train and test

# Split Data in train and validation
x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.25)

# created scaler
scaler = MinMaxScaler() #Normalization

# transform training dataset
x_train = scaler.fit_transform(x_train)
# transform test dataset
x_valid = scaler.fit_transform(x_valid)


3. Standardizing/Normalizing the data after splitting the data into train and test. Then fitting on the training set and then scaling on both train and test

# Split Data in train and validation
x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.25)

# created scaler
scaler = MinMaxScaler() #Normalization
# fit scaler on training data
scaler = MinMaxScaler().fit(x_train)

# transform training dataset
x_train = scaler.fit_transform(x_train)
# transform test dataset
x_valid = scaler.fit_transform(x_valid)

• In the third one, you are using fit_transform to scale the x_train and x_valid but it actually fits on your data again and scale rather than only scaling. If you just want to scale using previously fitted information use transform(). Oct 6, 2020 at 5:33
• @RAVITEJAM thanks, will make a note out of this :D Oct 7, 2020 at 8:19

Case I -
With a single scaling step, you might leak the test info into the train. In this approach you have a common min/max otherwise it would have been two pairs See, the plot for one of the Features of Iris dataset
Also, we don't scale the target. But I see this in your code. Case II -
This is fine but you should also consider the online cases, where you will not have a test set to scale the new test data.

Case III -
This is a better and an agnostic approach.
Your code implementation incorrect as suggested in the comment

• Thank you so much for this profound answer, I really appreciate it and it also clarifies better what I am looking for. Thanks once again :D Oct 7, 2020 at 8:18