# 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