Skip to content
ML Academy

Feature engineering and preprocessing · insight-0106

Make target encoding fold-safe with cross fitting; fit().transform() on training rows leaks

Evidence type
documented fact
Difficulty
medium
Roles
Data scientist, ML engineer
Industries
aviation, retail

Summary: situation, action, reason, boundary

  1. Situation

    You replace a categorical feature such as route or sales channel with the mean target per category.

  2. Action

    Encode training rows out of fold (scikit-learn TargetEncoder.fit_transform cross-fits), use the full-data mapping only for new rows, and keep the encoder inside the pipeline.

  3. Reason

    Out-of-fold encodings are computed without the row's own label, so the feature carries only information available for unseen rows.

  4. Boundary

    Mean, smoothed or Bayesian target encodings for regression and classification, especially with many small categories.

Symptom or risk

Each training row's encoding includes its own label, so the model learns a near-copy of the target; validation looks strong, production disappoints.

Exceptions

With very few, large categories leakage is small; one-hot may be simpler and equally good.

Trade-offs

Cross fitting adds noise to training encodings and costs compute; the alternative is optimistic validation.

Pitfalls

Calling fit(X, y).transform(X) on training data, which does not equal fit_transform(X, y).

Lessons and concepts

Claims and their sources