CS 230 — Mathematics, Statistics and Data for AI · Weeks 12–14
Missing data, preprocessing and leakage
Full teaching sequence and cumulative subject work
Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study scikit-learn: Imputation, Preprocessing, Pipelines and Common Pitfalls. Map the time each feature becomes available before constructing training rows.
Fitting preprocessing inside the experiment
Preprocessing learns information from data. Imputation, scaling, feature selection and vocabulary construction therefore belong inside the training process. In cross-validation, each fold’s transformer must be fitted on that fold’s training portion. Fitting once on all data before folding lets validation observations influence the representation used to predict them.
A final untouched test set estimates performance after model selection. Repeatedly checking it and changing the pipeline makes it part of development. Keep records of partition logic, especially for grouped or time-dependent observations.
Worked problem
Training values are [2,None,6]; held-out values are [100,None]. Compare training-only mean imputation with a mean fitted on all observed values.
Trace the complete reasoning
The training-only mean is 4. Replace missing values with 4 in both training and held-out data.
Using all observed values gives (2+6+100)/3=36. This allows the held-out feature distribution to alter the learned transformer.
Even without using labels, the evaluation no longer represents the declared training-only fitting process.
For cross-validation, repeat fitting within each fold. Preserve groups and chronological boundaries where the prediction setting requires them.
Independent transfer problem
Each customer has several rows. Random row splitting places the same customer in train and test. When is that a misleading evaluation?
Compare the transfer solution
It is misleading when deployment must generalize to unseen customers, because the test can benefit from customer-specific patterns already represented in training. Split by customer for that estimand. If deployment predicts future visits of known customers, a time-respecting design may be more appropriate. The correct split follows the intended deployment question.
Learning objectives
- Explain and trace the mechanisms in missing data, preprocessing and leakage.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
Study the mechanism
Missingness has meaning. A blank value can indicate not measured, not applicable, collection failure or a decision not to report. Replacing all blanks with zero imposes a substantive claim that may be false. Record missingness patterns and evaluate whether a missingness indicator is informative or merely encodes an unstable collection process.
Preprocessing is part of the model pipeline. Learn scaling parameters, imputation values and feature-selection rules from the training partition only, then apply them to validation and test data. Computing a mean from the entire dataset leaks information from the evaluation set into training. Even operations that do not use labels can transmit information about the future distribution.
Temporal leakage is particularly subtle. A field called final_invoice_status may be accurate but unavailable when the prediction must be made. A random split does not solve that problem. Define the prediction timestamp, feature availability and label observation window. Group leakage occurs when related records, such as repeated visits from one customer, appear on both sides of a split intended to represent new customers. Evaluation design must imitate the proposed use.
Worked example
training = [2.0, None, 6.0]
test = [100.0, None]
known = [x for x in training if x is not None]
fill = sum(known) / len(known)
train_filled = [fill if x is None else x for x in training]
test_filled = [fill if x is None else x for x in test]
assert train_filled == [2.0, 4.0, 6.0]
assert test_filled == [100.0, 4.0]The fill value is 4, learned only from known training values. The extreme test value does not shift it. This is a demonstration of partition discipline, not a universal endorsement of mean imputation. Categorical and time-series data need different considerations, and an all-missing training feature needs an explicit policy.
Check your understanding
Where should an imputation mean be learned?
- From the training partition only
- From the full dataset including held-out outcomes
- Independently from every test batch
Answer and reasoning for every option
Correct: From the training partition only Held-out data must not influence learned preprocessing.
Reconsider: From the full dataset including held-out outcomes Whole-dataset fitting contaminates the evaluation.
Reconsider: Independently from every test batch Refitting at inference changes the model and may create unstable behavior.
Transfer laboratory: Future information in preprocessing
Training covers January to June. A normalizer is fitted on January to December before a July test. Explain why excluding test labels does not make the procedure clean.
Work through the full solution
The fitted distribution uses future inputs unavailable at training time. The resulting normalization transmits information from the evaluation period into development. Fit transforms on the training window only and apply them unchanged to July. If real deployment updates transforms online, simulate that process using only data available before each prediction.
Evidence of understanding
Separate label leakage from distribution leakage and reproduce the actual time-availability policy.
Complete on-site teaching readings
These are full attributed source sections, not previews. Read them alongside the original Academy explanation and code.
- 2.2. Data Preprocessing Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 4.7. Environment and Distribution Shift Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 8.4. Imputation of missing values · The scikit-learn developers
- 8.3. Preprocessing data · The scikit-learn developers
- 8.1. Pipelines and composite estimators · The scikit-learn developers
- 12. Common pitfalls and recommended practices · The scikit-learn developers
Open the complete practice sequence and cumulative assessment →