ML2: Preparing data without fooling yourself

Lesson 2 of 5 · reading 15–25 minutes; practice and project time is on the module page

Leakage: when the answer slips into the inputs

🎯You'll be able to

Spot leakage from future information, from the target itself and from the way data was collected, and remove it.

🏠The everyday picture

A quiz where the answer sheet is printed on the back of the question paper gives perfect scores and teaches nothing. Leakage is an answer sheet hidden in your data: the model gets the answer from a column instead of learning the pattern.

📘What it really is

📝A worked example

Input: the starter kit's dakdost/features.py, which adds delay_hours to the features.

Steps and expected output (reference measurements, 2 November 2026):

  1. Train gradient boosting with delay_hours included: PR-AUC on June is 1.0, a perfect score.
  2. Check the data: ((df['delay_hours'] > 0).astype(int) == df['late']).mean() returns 1.0. The target is just "delay above zero".
  3. Permutation importance (ML7 Lesson 2) on May shows delay_hours at 0.919 and every other feature at 0.0.
  4. Remove delay_hours and train again: PR-AUC on June is 0.6235, a real result.

🌍Where it's used

📝Case scenario

A model to predict which online orders would be returned scored 0.98. Its most important feature was "return label printed". The label is printed after the customer asks for a return, so the model was predicting returns that had already started. Without it, the score was 0.71, which was still useful, and the team could finally trust it.

🖱️Try it

For each column of raw/shipments.csv, write when it becomes known: at booking, during delivery or after delivery. Success: you mark delivered_at and delay_hours as after delivery, and you write one sentence on how hub_load must be produced for it to be safe.

⚠️Common mistakes

🛠️For your project

Project 1's test 3 checks that no feature is known only after delivery or is personal; test 5 scores the model with those columns removed.

✅Quick check

What's the first thing to suspect when a score is almost perfect?

Show the answer

Leakage: a column that gives the answer away.

Why is delay_hours leakage?

Show the answer

It's only known after delivery, and it decides late exactly.

← PreviousSplitting data the way the model will be used
ProLearners Assist