DATA LEAKAGE
100% on the test set, half right in production
Obiora builds a router for Owoleaf's support desk that sends each ticket to one of four queues. He shuffles six weeks of labelled tickets, holds out a random quarter, and the router gets all 8 test tickets right. What do you expect on the next two weeks of new tickets?
An evaluation estimates how the model does on data it has not seen. Leakage is anything that lets information from the test set into training, so the estimate is of something easier. Kaufman and colleagues (“Leakage in Data Mining”, KDD 2011) define it as information the model should not legitimately have when it predicts. For tickets there are three routes: copies of the same text on both sides, the same customer on both sides, and training data from after the test period. None of them raises an error. The score is too high, and you find out in production.
# One shuffle over every ticket.
train, test = train_test_split(tickets, test_size=0.25, random_state=0)
# Follow-ups, double submissions and copy-pasted error messages
# land on both sides. Tickets from next month train the model
# that is scored on this month.# 1. Split by time: train on the past, test on what came after.
train = [t for t in tickets if t.day < CUT]
test = [t for t in tickets if t.day >= CUT]
# 2. Split by group: a customer (or thread) lives on one side.
seen = {t.customer for t in train}
test = [t for t in test if t.customer not in seen]
# 3. Dedupe across the split: exact and near-duplicate.
test = [x for x in test if not any(jaccard(tri(x), tri(t)) > 0.3 for t in train)]
# 4. Keep a held-out set that no one tunes on; look at it to ship. The router is a 1-nearest-neighbour lookup: it answers with the queue of the most similar training ticket, by shared content words, and abstains (scored wrong) when no word is shared. It memorises, which makes leaks visible. Watch the LEAK AUDIT box at the train step and the eval − production line at the end. RANDOM BY TICKET scores 100% against 42% in production. RANDOM BY CUSTOMER keeps customers apart and still scores 100% against 50%: near-copies from different customers (two card top-up complaints and the Error 1042 message pasted after an app update) land on both sides, and it trains on tickets from after the test period. TIME ONLY looks close, 55% against 50%, but customer c18’s follow-up crosses the cut and two test tickets have a near-copy in training. With 11 test tickets one ticket is 9 points, so the leak is in the number even when the gap is small.
The FIX pipeline is honest: eval and production agree within 10 points. Insert one step somewhere in it so the eval overstates production by more than 10 points again. Both faults are things teams do with good intentions.
GOAL: shipped, and eval − production > 10 points
Write split(tickets, nearDup). Each ticket is { id, day, cust, label, text }; nearDup(a, b) is true when two tickets' word-trigram Jaccard is above 0.3. Return { train, test }, two arrays of tickets (or of ids). The grader runs it on the 35 tickets, on the same tickets in reverse order, and on a set with one more late ticket from an early customer. It checks: no ticket is on both sides and every ticket is one of the input's; every training ticket is dated before every test ticket; no customer is on both sides; no test ticket is a near-duplicate of a training ticket; test holds at least 20% of the input and train at least 50%.
The fix does not make the router better. It scores 44% on its test set and 50% in production: a bad model, measured honestly, which is the point. With 9 test tickets the two numbers are one ticket apart. A toy this small exaggerates every effect; on real data the gap is usually smaller and no less real.
Each rule comes from a way the data is correlated. Split by time when the model will predict the future; Kaufman and colleagues call this the no-time-machine case and enforce it by timestamping every row and cutting at a point in time. Split by group (customer, thread, patient, device) when rows in a group are related. Dedupe across the split, exact and near-duplicate, because the same text can arrive from different groups. The fine-tuning chapter’s data script drops generated tickets whose word trigrams overlap a test ticket above 0.3, the same check the sim uses.
Then keep the test set out of every decision but the last one. If you try prompts or settings (a prompt wording, a learning rate, a warmup length) until the eval passes, the eval has become training data. The building an eval set chapter keeps a held-out portion you look at only to decide whether to ship, and the evals in CI chapter keeps a held-out slice nobody tunes against.
Ticket routers, intent classifiers, fraud and churn models, fine-tuning data that overlaps the eval set, LLM evals built from production logs where the same conversation appears twice, and benchmarks that turn up in a model's pretraining data. The symptom is always the same: an offline score that production never matches, often with no change in the model between them.
“Your model scored 95% offline and 70% in production. What happened?” Ask how the split was made before you blame drift. Name the routes: duplicates and near-duplicates across the split, the same user on both sides, a random split on time-ordered data, preprocessing or feature selection fitted on all the data, and tuning on the test set. Give the fixes: a time split, a group split, cross-split dedupe with exact hashes and n-gram Jaccard, preprocessing fitted on train only, and a held-out set nobody tunes on.
USED IN
Chapters that build on this lesson.
- AI engineering philosophies: evals first, the simplest thing that works, the model will changeAI Engineer
- Scoping an AI featureAI Engineer
- Forecasting, and saying how wrong it may beData and ML
- LLMs in data work: evals, extraction and text as dataData and ML
- Monitoring models and driftData and ML
- Taking a model from notebook to productionData and ML
- AI in network operations: prediction, optimisation and what to verifyHow Mobile Networks Work
- Gradient-boosted credit models and explaining a decline to the customerLending and Credit
- Machine-learning fraud scoring at authorisation: features, latency budgets and false declinesPayments