All lessons in Machine Learning Engineering: Reliable Predictive Models

Leakage, Groups, and Honest Splits

Choose a split matching future use and exclude information unavailable at prediction time.

30 minutes · Free lesson

What you will learn

  • Choose row, group or time-based splits that match a stated deployment question.
  • Identify leakage through unavailable features, repeated entities and premature labels.
  • Separate fitting, model selection and final evaluation in an experiment record.

A high validation score answers a question about the way validation rows were held out. If the split hides a parcel but leaves near-identical records from the same journey in training, it may estimate performance on familiar journeys rather than new ones. If it mixes December and January at random, it may tell you little about predicting January using only what was known in December.

Evaluation design begins with the prediction contract. Decide what must be new at deployment: a row, a customer, a depot, a time period, or a combination. Then construct the split to represent that novelty. Choosing a splitter because it is convenient can make the entire model comparison answer the wrong question.

Three information roles

Training rows fit parameters, including preprocessing and feature selection. Validation or cross-validation results guide model choice and other decisions such as thresholds. The final test is reserved for an evaluation of the selected procedure after those choices are fixed.

A final test stops being sealed once its results influence a decision. Renaming the file does not restore its role. If the team repeatedly adjusts models after inspecting it, report that usage and obtain an appropriate fresh evaluation rather than presenting the best observed score as untouched evidence.

The split manifest should record row identities, the sampling unit, date boundaries or group rules, and the label-maturity cutoff. Retaining only a random seed is weaker: a changed row order or filtering step can produce a different split with the same seed.

Random rows: useful under a specific question

A random row split can be appropriate when rows are independent draws from a comparable population and the future task involves more such rows. Stratification approximately preserves class proportions, helping ensure rare classes appear in each split. It does not remove dependencies, correct a changing population or make a small positive sample large.

The workbook uses independent synthetic rows and a stratified 60/20/20 split. Those assumptions are explicit so the experiment can focus on model behavior. It does not contain real depot or timestamp fields, and row numbers must not be relabeled as time to manufacture a temporal validation claim.

Worked example

Counting the actual sampling units

  1. A dataset has 1,000 parcel scans from 100 journeys, with ten scans from each journey. The target is shared by scans from the same journey.
  2. A random row split assigns 800 scans to training and 200 to validation. A validation scan may have nine close relatives represented in training.
  3. If deployment concerns entirely new journeys, split by journey ID: for example, 80 journeys for training and 20 for validation, retaining every scan from a journey together.
  4. The resulting sizes are still 800 and 200 scans in this balanced fixture, but the information boundary is different. Equal row counts do not imply equivalent evaluation.
  5. If deployment instead predicts another scan from an already-known journey, the evaluation question changes. Define which observations from that journey would already be available and respect their ordering.

Groups: decide what must remain together

A group split assigns each group wholly to one side of the boundary. It can test generalization to unseen customers, devices or depots. Selecting the group requires domain knowledge. Grouping by customer may still leave duplicated shipment records across splits; grouping by shipment may still overstate performance at unseen depots.

Some tasks involve overlapping relationships. Two customers can share a household, or several shipments can share a journey and a driver. A single ID may not block every route by which dependent information crosses. Audit the relationships relevant to the claim and describe any remaining dependence. A lower score from a harder group split is not evidence that the model became worse; it may reflect a more demanding question.

Time: respect feature and label availability

A chronological split trains on earlier data and evaluates later data. At each training cutoff, include only labels that would already be known. A parcel dispatched just before the cutoff may not yet have reached its promised window. Treating an unresolved outcome as on time creates biased labels.

Time-series splitters arrange observations by order. They do not automatically understand label delays, overlapping forecast horizons or when a backfilled feature became available. A gap between training and validation can reduce some boundary contamination, but its duration must follow the task's information delays, not a universal rule.

Worked example

A training cutoff with delayed labels

  1. A model is rebuilt at midnight on June 10. Each parcel's label becomes available exactly 48 hours after dispatch in this simplified fixture.
  2. A June 7 midnight dispatch has a label available June 9 midnight, so it is eligible for training at the rebuild.
  3. A June 9 midnight dispatch is not labeled until June 11 midnight. Its features may exist on June 10, but its outcome cannot be used for supervised fitting then.
  4. A dispatch exactly at June 8 midnight matures exactly at the cutoff. State whether the contract permits timestamps equal to the cutoff; an exclusive rule would omit it, while an inclusive rule needs evidence that ingestion completed by that instant.
  5. Evaluate on dispatches after the rebuild and wait for their labels to mature before scoring. The evaluation report's date is later than its prediction date.

Leakage can survive a perfect split

A delivery-completed flag leaks the future even if train and validation row IDs are disjoint. An encoder fitted using all labels leaks outcomes even if the final estimator sees training labels only. A duplicated record can cross the boundary under a different ID. These are different mechanisms and require different checks.

Inspect candidate features against the prediction-time contract. Fit learned transformations only within the training portion of each split. Check duplicate or near-duplicate groups. Review unexpectedly strong features and suspicious performance jumps. None of these checks proves there is no leakage, but each targets a concrete failure mode.

A common mistake is to infer that a split is correct because the class proportions match. Another is to choose the evaluation design giving the best score. The split represents a claim; it should follow the intended use rather than compete as another hyperparameter.

Combining future time and unseen groups

If the model will serve new depots next quarter, both boundaries matter. One design holds out selected depots entirely and evaluates their future-period records, while fitting on earlier eligible records from other depots. Also report a separate future-period evaluation on known depots if that is a substantial use case.

This reduces available training and evaluation data, particularly when few groups exist. Document that limitation rather than relaxing the split silently. Report the number of independent groups as well as rows, and avoid treating a thousand records from one new depot as evidence about a thousand independent new depots.

Exercise

There are 50 customers with 20 deliveries each. The deployment claim is performance on new customers. A teammate uses a stratified row split with 800 training and 200 validation rows. What is the flaw, and what split information should the manifest include?

Check your answer

The same customer can appear on both sides, so the split does not directly test unseen-customer performance. Assign whole customers to splits, retaining all their deliveries together. Record customer membership, exact row IDs, class counts and the rationale for the group choice. Twenty held-out customers and 200 held-out deliveries are different quantities; do not infer one from the other without checking group sizes.

Exercise

A parcel feature has event time July 3 and became available July 6. A model trained as of July 5 uses the value after a historical join on event time alone. Is the join valid? State the missing condition.

Check your answer

It is invalid for a July 5 information boundary because the value was not available until July 6. The join must check availability time as well as the relevant event-time rule, using the version that could actually have been known at the training or prediction cutoff.

Exercise

A team evaluates twelve model variants on a final test and chooses the best. It then reruns that selected variant with the same seed and obtains the same test score. Has the test become an independent final estimate again?

Check your answer

No. The first comparison used test outcomes for selection. Repeating the same computation demonstrates reproducibility, not fresh statistical evidence. Record the selection usage and evaluate the fixed procedure on an appropriately reserved new sample or a properly designed outer evaluation.

Further reading

Summary

A split states what is new at evaluation. Match rows, groups and time boundaries to the deployment claim, enforce label maturity and feature availability, and retain a manifest. Final evaluation remains separate from adaptive selection even when every computation is reproducible.

Next action

Design two split manifests for the parcel task: one for future deliveries at known depots and one for future deliveries at unseen depots. State which labels and features are eligible at each training cutoff.

Choose another lesson