Splits, cross-validation & leakage

Time-based vs random splits, group leakage, target leakage through features computed after the label. Leakage stories separate practitioners from course-takers.

About 28 min read3-question knowledge check

The mental model

A held-out split is a rehearsal for the future. The model will be judged on data it has never seen, arriving after it was trained — so evaluation must simulate exactly that: unseen entities, later timestamps, no whispers from the answer sheet. Leakage is any channel through which the future (or the label) whispers into training or evaluation. The analogy: a student who found the exam paper in the photocopier. Their practice scores are perfect and mean nothing — and crucially, they may not even know they cheated, because the answers arrived disguised as "extra study material." That is leakage's signature: everyone is acting in good faith, the offline numbers are stellar, and the model faceplants on live traffic.

Why this exists as a discipline: the default tools betray you. train_test_split shuffles uniformly — correct only when examples are independent and the world is static. Real data violates both: rows from the same user/hospital/device are near-duplicates (group structure), and the world drifts (time structure). Shuffle-splitting such data lets the model meet the same entity, or the future, on both sides of the split. The resulting metrics are not slightly optimistic — they are frequently fantasy, off by tens of points.

The one-sentence test that catches most leakage: "At the moment of prediction in production, would this exact value be available?" Ask it of every feature, every preprocessing statistic, every join. If the answer involves information dated after the prediction timestamp, or derived from the label, or computed over data that includes the test rows — it leaks.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision