DATA LEAKAGE

100% on the test set, half right in production

L214 MINAI SYSTEMS
PREDICT

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.

▶ SIMULATION
SPLIT
—
train 0 (—)
test 0 (—)
LEAK AUDIT
train tickets dated on/after a test ticket: —
customers on both sides: —
test tickets with a near-copy in train: —
SCORES
eval accuracy —
production accuracy —
eval − production —
model1-nearest neighbour on content words; abstains (wrong) when no word is shared
✓ at training time: no training ticket is dated on or after a test ticket✓ at training time: no customer has tickets on both sides✓ at training time: no test ticket nearly duplicates a training ticket✓ after shipping: eval accuracy ≤ production accuracy + 10 points
SCHEDULE
01[DATA]load 35 labelled tickets, days 2–42
02[SPLIT]split by ticket: hash(ticket id) mod 4 = 0 goes to test
03[MODEL]train: memorise the training tickets (1-nearest neighbour)
04[MODEL]evaluate on the test set
05[PROD]ship; score the 12 production tickets from days 44–54
Start of random by ticket scenario. 5 steps.
# 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.

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

A test score is only as honest as the wall between training and test. Rows are not independent: copies, follow-ups, customers and time all reach across a random split. Cut by time, keep each group on one side, remove near-copies, and look at the held-out set only to decide. If eval and production disagree, suspect the split before the model.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.