Why must feature transformation parameters be learned only from training data?

Quick Overview

Asks why preprocessing parameters such as scaling statistics, imputation values and encodings should be fit on the training split alone. Tests understanding of data leakage, honest model evaluation, train/serve consistency and correct use of pipelines in cross-validation.

Why must feature transformation parameters be learned only from training data?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Preprocessing steps such as scaling, imputation and categorical encoding have parameters that are estimated from data. Why must feature transformation parameters be learned only from training data? ```hint Think of the test set as next month Treat the test rows as data that will arrive in the future. Which statistics would you actually have when you fit the transformer? ``` ### What a Strong Answer Covers - The role of validation and test data as a stand-in for unseen future data - Examples of learned preprocessing parameters, from scaling statistics to target encodings and selected features - How fitting on all the data biases the evaluation, and which transforms make that bias large or small - The link to production and train/serve skew - The practical pattern: fit on training data, only transform elsewhere, and refit per fold with a pipeline ### Follow-up Questions - Is it a leak to fit an unsupervised transform such as PCA on the combined train and test features? How big is the effect likely to be? - How do you compute target encoding for the training rows themselves without leaking each row's own label? - In a time-series problem, what extra rule applies when fitting transformers? - Your production scaler was fit a year ago and the feature distribution has drifted. What do you do?

Overview: Asks why preprocessing parameters such as scaling statistics, imputation values and encodings should be fit on the training split alone. Tests understanding of data leakage, honest model evaluation, train/serve consistency and correct use of pipelines in cross-validation.

Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Preprocessing steps such as scaling, imputation and categorical encoding have parameters that are estimated from data. Why must feature transformation parameters be learned only from training data?

What a Strong Answer Covers Guidance

  • The role of validation and test data as a stand-in for unseen future data
  • Examples of learned preprocessing parameters, from scaling statistics to target encodings and selected features
  • How fitting on all the data biases the evaluation, and which transforms make that bias large or small
  • The link to production and train/serve skew
  • The practical pattern: fit on training data, only transform elsewhere, and refit per fold with a pipeline

Follow-up Questions Guidance

  • Is it a leak to fit an unsupervised transform such as PCA on the combined train and test features? How big is the effect likely to be?
  • How do you compute target encoding for the training rows themselves without leaking each row's own label?
  • In a time-series problem, what extra rule applies when fitting transformers?
  • Your production scaler was fit a year ago and the feature distribution has drifted. What do you do?