thebench.bio #1 Ecosystem for the Bio-Tech Industry
All Machine Learning questions

Question 11 of 20 Fundamentals Practical

Why should feature scaling sit inside a scikit-learn Pipeline?

Short answer

A scaler learns means and standard deviations from the data it is fitted on. Inside a Pipeline it is refitted on each training fold only and then applied to the held-out fold, so test data never shape the preprocessing and the same transform is always applied at prediction time.

Why

StandardScaler subtracts a mean and divides by a standard deviation that it estimates in fit. Fitting it on the full dataset before cross-validation lets every test fold contribute to those estimates, a small leak. The scikit-learn common pitfalls page shows the opposite error too: training on scaled data and then predicting on unscaled test data, which gives nonsense predictions.

A Pipeline chains the scaler and the model into one estimator. Calling cross_val_score or GridSearchCV on the pipeline fits the scaler on each training split and only transforms the validation split. The scikit-learn guide lists this safety as a main reason to use pipelines, alongside convenience and joint tuning of every step.

For scaling alone, the leak is often small. It becomes serious for steps that look at the target or select features, such as SelectKBest, and the pitfalls page shows feature selection on all data producing accuracy well above chance on random labels. Putting every data-dependent step in the pipeline is a habit that removes the whole class of error.

It also means the saved model carries its preprocessing, so a colleague scoring new samples cannot forget to scale them. Steps that do not learn from data, such as a fixed log transform, are safe either way.

A strong answer covers

  • Scalers learn statistics in fit
  • Pipeline refits on each training fold only
  • Same transform applied at prediction
  • Feature selection leaks far more than scaling
  • Saved pipeline carries its preprocessing

Common mistakes

  • Calling fit_transform on the whole dataset and then running cross-validation on the result
  • Scaling training data but passing raw values to predict on new samples