Data leakage occurs when information that should be unavailable at prediction time influences training, preprocessing, validation, or testing. It makes evaluation look stronger than likely deployment performance. A hold-out test set is meant to simulate unseen data, so it must remain separate from every modeling stage until final evaluation. section 8.1.3
Two forms matter when preprocessing categorical features. Feature leakage lets feature information cross from training into the test set. Target leakage lets labels cross that boundary. Target encoding is especially risky because it replaces a category with a statistic, such as the mean target value for rows in that category. If labels are used carelessly to produce encodings, the model can indirectly receive information it should not have. section 8.1.3
Figure 8.3 Target leakage from the training to test set illustrated. All the targets (labels) in the training set are greedily used to create an encoding for red, which is used to encode this category in both the training and test sets, leading to target leakage.
This illustration shows target leakage: labels from the training partition determine the encoding for red, and that encoding is then used across both partitions. section 8.1.3
Guard against leakage by preserving the intended train/test boundary throughout preprocessing and evaluation:
- Split features and labels into training and test data before fitting transformations. section 8.1.4
- Fit a preprocessing transformer on training features, then transform test features with that fitted transformer rather than fitting it again. section 8.1.4
- Use pipelines to combine preprocessing and modeling consistently, including imputation, scaling, and categorical encoding. section 8.3.2
- Keep the final test set for final performance evaluation while using cross-validation and parameter search during model selection. section 8.3.2
Greedy target encoding uses all available training labels to compute category statistics. Although it can yield a compact representation, it can leak target information and can also create distribution shift between encoded training and test data. Hold-out encoding avoids both problems by reserving a third partition exclusively for target-statistic encoding. Leave-one-out encoding avoids reserving that extra subset by excluding the individual training example being encoded; test data is encoded with greedy target encoding. section 8.1.3
Figure 8.5 Hold-out encoding partitions the available data into three sets: training and test, as usual, and a third hold-out test set to be used exclusively for encoding with target statistics. This avoids both target leakage and distribution shift.
Hold-out encoding adds a dedicated encoding partition, separating the labels used for target statistics from ordinary training and test roles. section 8.1.3
Categorical credit-approval pipeline
For binary credit approval, continuous columns can be standardized while categorical columns are encoded through separate pipelines joined by a ColumnTransformer. The transformer is fit on training data and applied to test data. One-hot encoding expanded the representation to 38 features and produced 89.855% test accuracy in the shown run; greedy target encoding used 14 features and produced 91.304%, while leave-one-out target encoding used 14 features and produced 90.580%. These results compare encoders, but the pipeline boundary is what keeps preprocessing aligned with evaluation. section 8.1.4








