10
$\begingroup$

I was following the book "Hands-On Machine Learning with Scikit-Learn & TensorFlow" by "Aurelien Geron".

The following remark was made about feature scaling : -

As with all the transformations, it is important to fit the scalers to the training data only, not to the full dataset (including the test set). Only then can you use them to transform the training set and the test set (and new data)

My understanding of the above text is that feature scaling is done only on the training and not on the test set. Is this interpretation correct?

In case yes, what is the rationale behind not using feature scaling for test dataset?

$\endgroup$

3 Answers 3

21
$\begingroup$

Not quite. You learn the means and standard deviation of the training set, and then:

  • Standardize the training set using the training set means and standard deviations.
  • Standardize any test set using the training set means and standard deviations.

This is just following the general principle: any thing you learn, must be learned from the model's training data.

$\endgroup$
2
  • 3
    $\begingroup$ But why not scaling full data beforehand (before train test split)? What will happen if I scale full data? $\endgroup$
    – Ankit Seth
    Commented Jul 7, 2018 at 14:18
  • 8
    $\begingroup$ Because you have implicitly learned the mean of the testing data by including it in your calculation. If you want to test your model honestly, you can't look at the target in your testing data in any part of constructing the model. Remember, when your model is in production, you won't even know the target in your data. $\endgroup$ Commented Jul 7, 2018 at 15:35
5
$\begingroup$

To follow up on a comment made to the answer - If you scale the data before train/test split you will get data leakage. Calculating mean/sd of the entire dataset before splitting will result in leakage as the data from each dataset will contain information about the other set of data (through the mean/sd values) and could influence prediction accuracy and overfit.

$\endgroup$
0
$\begingroup$

Data might already be naturally scaled by the experiments and or conventions used (eg predicting percentages). Secondly, you'll have a system with two variables. So, you cannot readily compare to "unscaled" learning with scaled learning.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.