Lawfare Group → Knowledge Institute → Lawfare Academy
← CS 230 — Mathematics, Statistics and Data for AI
Weeks 12–14
Missing data, preprocessing and leakage
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.
- Test the added failure-mode fixture and explain which guarantee the evidence does and does not establish.
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 reasoning 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.
Extended teaching and laboratory
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.
Step-by-step reasoning 1. 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.
Failure-oriented laboratory: Group leakage, missingness and deployment-time availability
A dataset split is part of the measurement design. If multiple rows belong to one customer, patient, device or document family, a random row split can place closely related observations in both training and evaluation. The resulting score may measure recognition of groups already seen rather than generalization to new groups. Choose the split unit to match the deployment question.
Preprocessing must also respect the boundary. Fit imputation, scaling and feature selection on training data, then apply the learned transformation to held-out data. Computing a fill value from the complete dataset leaks evaluation information even when labels are not directly used. Missingness can carry information, but its meaning may change when collection processes change.
Finally ask whether each feature exists at prediction time. A field created after an outcome is not a legitimate predictor for an earlier decision. Document availability timestamps, group identifiers and the target horizon. A reproducible report includes rejected features and reasons, not just the winning model and score.
Worked fixture
A fixture contains 100 customers with 10 rows each. A random row split puts nearly every customer on both sides. A feature named final_resolution is created after the outcome being predicted.
Explain the fixture
Use a customer-disjoint split when the task is prediction for previously unseen customers. Remove final_resolution because it is unavailable at decision time. A row split may still answer a different repeated-customer question, but that deployment target must be explicit. Never select whichever split yields the most attractive score.
Apply the idea
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.
Each customer has several rows. Random row splitting places the same customer in train and test. When is that a misleading evaluation?
Additional laboratory — Write assertions that train/test group intersections are empty, all fitted preprocessing uses training rows and feature timestamps precede the prediction cutoff. Compare a group split and a time split for their different questions.
Read the worked reasoning
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.
Additional laboratory model — Assertions must fail when a duplicate group or future-created feature is deliberately inserted. A group split tests new entities; a time split tests later periods and may include recurring entities. Neither automatically substitutes for the other. State both the inference target and remaining distribution-shift risks.
Check understanding
Where should an imputation mean be learned?
- From the training partition only
Feedback
Held-out data must not influence learned preprocessing.
- From the full dataset including held-out outcomes
Feedback
Whole-dataset fitting contaminates the evaluation.
- Independently from every test batch
Feedback
Refitting at inference changes the model and may create unstable behavior.
Further reading
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.
- 2.2. Data Preprocessing Open the notebook in SageMaker Studio Lab
- 4.7. Environment and Distribution Shift Open the notebook in SageMaker Studio Lab
- 8.4. Imputation of missing values
- 8.3. Preprocessing data
- 8.1. Pipelines and composite estimators
- 12. Common pitfalls and recommended practices
- Primary documentation for Group leakage, missingness and deployment-time availability
- Academy — runnable synthetic lab kit and instructions