How does out-of-fold target encoding prevent target leakage during training?

Quick Overview

Asks how out-of-fold target encoding keeps a categorical feature from leaking the training label into the model. Tests your understanding of target encoding, cross-fitting, smoothing for rare categories, and how train-time and inference-time encodings should be computed.

How does out-of-fold target encoding prevent target leakage during training?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Target encoding replaces a categorical value with a statistic of the target for that category, such as the mean label. How does out-of-fold target encoding prevent target leakage during training? ```hint Start from the failure Work out what a naive mean encoding gives for a category that appears only once in the training set. ``` ### What a Strong Answer Covers - What target leakage looks like with naive target encoding, especially for rare categories. - The K-fold procedure, and how validation, test and serving rows are encoded. - Why matching the train-time and inference-time feature distribution stops the model from over-trusting the feature. - Smoothing, unseen categories, and grouped or time-ordered data. - Where the method still goes wrong, such as leave-one-out encoding or an encoder fit outside the cross-validation loop. ### Follow-up Questions - Why can leave-one-out target encoding still leak even though each row's own label is excluded? - How does the choice of K affect the encodings the model trains on? - How would you target-encode a feature for a time-series problem? - In scikit-learn, why does `TargetEncoder().fit(X, y).transform(X)` give different values from `fit_transform(X, y)`?

Overview: Asks how out-of-fold target encoding keeps a categorical feature from leaking the training label into the model. Tests your understanding of target encoding, cross-fitting, smoothing for rare categories, and how train-time and inference-time encodings should be computed.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Target encoding replaces a categorical value with a statistic of the target for that category, such as the mean label. How does out-of-fold target encoding prevent target leakage during training?

What a Strong Answer Covers Guidance

  • What target leakage looks like with naive target encoding, especially for rare categories.
  • The K-fold procedure, and how validation, test and serving rows are encoded.
  • Why matching the train-time and inference-time feature distribution stops the model from over-trusting the feature.
  • Smoothing, unseen categories, and grouped or time-ordered data.
  • Where the method still goes wrong, such as leave-one-out encoding or an encoder fit outside the cross-validation loop.

Follow-up Questions Guidance

  • Why can leave-one-out target encoding still leak even though each row's own label is excluded?
  • How does the choice of K affect the encodings the model trains on?
  • How would you target-encode a feature for a time-series problem?
  • In scikit-learn, why does TargetEncoder().fit(X, y).transform(X) give different values from fit_transform(X, y) ?