Why does constructing group-level aggregate features using the entire dataset introduce data leakage?

Quick Overview

Explains why group-level aggregate features such as per-user means or per-category target rates leak information when computed over the whole dataset. Tests understanding of train/test contamination, target self-inclusion, future information, and leak-free ways to build these features.

Why does constructing group-level aggregate features using the entire dataset introduce data leakage?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Why does constructing group-level aggregate features using the entire dataset introduce data leakage? Typical examples are a per-user mean of some value or a per-category target rate, computed once over all rows and joined back onto each row as a feature. ```hint Prediction time For a single training row, list which rows went into its aggregate value, then ask whether each of those rows (and their labels) would exist at the moment the model makes that prediction in production. ``` ### What a Strong Answer Covers - How an aggregate over all rows carries validation and test information into training features, and what that does to offline metrics. - Why target-based aggregates include the row's own label, and why small groups make this worst. - The time dimension: all-time aggregates that include events after the row's timestamp. - A concrete fix for each source of leakage, including what value unseen groups get at prediction time. ### Follow-up Questions - How would you implement out-of-fold target encoding inside a cross-validation loop so that no fold's validation labels reach its own encoding? - Why can leave-one-out target encoding still leak with tree models, and what would you do about it? - How would you choose the smoothing strength toward the global mean? - In a feature store that serves both training and online inference, how do you guarantee point-in-time correct aggregates?

Overview: Explains why group-level aggregate features such as per-user means or per-category target rates leak information when computed over the whole dataset. Tests understanding of train/test contamination, target self-inclusion, future information, and leak-free ways to build these features.

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

Why does constructing group-level aggregate features using the entire dataset introduce data leakage?

Typical examples are a per-user mean of some value or a per-category target rate, computed once over all rows and joined back onto each row as a feature.

What a Strong Answer Covers Guidance

  • How an aggregate over all rows carries validation and test information into training features, and what that does to offline metrics.
  • Why target-based aggregates include the row's own label, and why small groups make this worst.
  • The time dimension: all-time aggregates that include events after the row's timestamp.
  • A concrete fix for each source of leakage, including what value unseen groups get at prediction time.

Follow-up Questions Guidance

  • How would you implement out-of-fold target encoding inside a cross-validation loop so that no fold's validation labels reach its own encoding?
  • Why can leave-one-out target encoding still leak with tree models, and what would you do about it?
  • How would you choose the smoothing strength toward the global mean?
  • In a feature store that serves both training and online inference, how do you guarantee point-in-time correct aggregates?