esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

What Is Data Leakage?

What data leakage is, where it hides in a real project and how to stop it, explained simply for your AI interview.

In short

Data leakage is when information that would not exist at prediction time sneaks into training, so the model's scores look better than its real results. Common causes are scaling on all the data, features filled in after the outcome, and random splits of time-ordered data.

11 min readIntermediateCommon · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Data leakage is when information that would not be there at prediction time sneaks into training, or when test data helps build the model. The score then looks better than real use will be. The usual culprits are scaling on all the data, features filled in after the outcome, and random splits of time-ordered data. My fix is to split first, use pipelines, and split by time or by customer where rows are linked.

What is data leakage, in plain words?

A model learns from training data, then we check it on rows it has never seen. Data leakage is when information from the test rows, or something that will not be known at prediction time, gets into that learning. The model then looks better on paper than it will in real use.

Think of a job interview where the candidate was given the questions and answers the night before. Their score looks brilliant, but it says little about how they will work on Monday. A leaky model is that candidate: it scores well for the wrong reason. In an AI project, that is a model tested on rows it has already learned from, as in the lender example later on.

  • “Data leakage occurs when information that would not be available at prediction time is used when building the model.”

    scikit-learn

  • “Data leakage is a spurious relationship between the independent variables and the target variable that arises as an artifact of the data collection, sampling, or pre-processing strategy.”

    Kapoor and Narayanan, 2022

Key words

Test data
Rows held back to check the model. They stay unseen until the final check.
Preprocessing
Cleaning or scaling steps that learn a number, such as an average, before training.
Prediction time
The moment the model is used on a new case, such as a new loan application.
Pipeline
A saved chain of steps that run in a fixed order, so each one learns only from the rows it should.
Fit
Let a step learn its numbers from data, such as an average.

Where does data leakage hide in a real project?

Four common places where leakage hides. Each one looks harmless when you write it, which is why it gets past reviews.

  • Preprocessing fitted on all rows

    A scaler (a step that rescales numbers), an imputer (a step that fills in blanks) or a feature picker learns from every row, test rows included, before the split. Scikit-learn warns that the test subset then influences the model.

  • A feature known only after the outcome

    A column filled in after the thing we predict has already happened. The model learns from the answer itself, so its score says little about real cases. Kapoor and Narayanan name a proxy (a column that stands in for the outcome) as one way leakage happens.

  • A random split of time-ordered data

    Shuffling three years of loans mixes the future into training. The model then learns from months that come after the ones it is tested on, which it will never have in real use.

  • The same customer on both sides

    One borrower's loans, or one patient's visits, land in both train and test. The model learns to recognise the person, so it misses the wider pattern.

In 2022 Kapoor and Narayanan surveyed 20 papers that identify pitfalls across 17 fields, and those pitfalls collectively affect 329 papers. They built a taxonomy of 8 types of leakage that range from textbook errors to open research problems. They say data leakage is a widespread problem that has led to severe reproducibility failures.

  • “We show that data leakage is indeed a widespread problem and has led to severe reproducibility failures.”

    Kapoor and Narayanan, 2022

What does a leaky pipeline look like next to a clean one?

Leaky and clean preparation

Leaky way

Scale all rows together

test rows shape the average

Then split train and test

too late to undo the leak

Clean way

Split train and test first

test rows stay hidden

Fit scaler on train only

test rows only get transformed

Score looks too good

can drop once it goes live

Honest score

close to real use

  1. 1The test rows now count toward the average before any split.
  2. 2The model is built on that mixed data, so its score looks great.
  3. 3The split comes first, so the test rows stay hidden from every step.
  4. 4The scaler learns from training rows only, then just transforms the test rows.
Two ways to prepare the same data. The leaky way scales every row first, so the test rows shape the average before the split. The clean way splits first and learns the scaling from training rows only.

Here is a worked example with made-up numbers. A mid-size lender builds a loan default model on 400,000 loans from 2019 to 2025. The team splits the rows at random and gets 91 percent accuracy on a balanced test set, half defaults and half repaid. One feature, a collections call logged, ranks first. Collectors only log those calls after a borrower misses a payment, so the feature describes the outcome, and it is unknown when the application arrives.

In the first month of live use the model is right about 62 percent of the time, on the same balanced measure. The fix has three parts: drop the late feature, split by application date, and keep each borrower on one side of the split. The honest score falls to about 70 percent, lower on paper and far more useful to the lender.

How do you spot and fix data leakage?

scikit-learn's own guide shows the danger with a deliberately plain case. It builds a classification problem from 10,000 random features, then keeps the best features using all the rows. The score comes out much higher than chance, even though the targets are random. With the split first, the score drops to close to chance.

Watch for three warning signs: a score that is too good to be true, one feature that dominates the model, or a big gap between validation results and live results.

  1. 1

    Split first

    Set the test rows aside before any cleaning, scaling or feature choice. The split is the first thing in the pipeline.

  2. 2

    Fit inside a pipeline

    Put scaling and feature selection in one pipeline, so each step learns only from the training rows.

  3. 3

    Split by the real order

    For time data, train on the past and test on later months, the way the model will be used.

  4. 4

    Keep linked rows together

    Keep every loan of one borrower, or every visit of one patient, on the same side of the split.

  5. 5

    Drop late features

    Remove any column that is filled in after the outcome happens, even if it makes the score jump.

Tip

Ask one question for each feature: would I have this on the day the prediction is made? If not, it leaks.

Here is the first mistake in a few lines of Python: the average is worked out on every row, test rows included. The made-up loan amounts are small, and one unusually large loan sits in the test set.

Python
# A made-up loan book (amounts in thousands). The test set holds one unusually large loan.
train = [4.0, 5.0, 6.0, 5.0]
test = [30.0]

def mean(xs):
    return sum(xs) / len(xs)

leaky_mean = mean(train + test)  # wrong: the test loan shapes the average
clean_mean = mean(train)         # right: learn the average from training rows only

print("leaky average:", leaky_mean)
print("clean average:", clean_mean)
print("test loan gap, leaky way:", test[0] - leaky_mean)
print("test loan gap, clean way:", test[0] - clean_mean)

Output

leaky average: 10.0
clean average: 5.0
test loan gap, leaky way: 20.0
test loan gap, clean way: 25.0

The leaky average of 10.0 is pulled up by the one large test loan, so that loan looks only 20 above it instead of 25 above the clean average.

How would you check a model someone else built for leakage?

Start with how the data was split, then go feature by feature. Two questions help catch leaks in a review.

  1. 1

    Read the split

    Was it random, by date, or by customer? A random split of time or linked rows is the first thing to fix.

  2. 2

    Time each feature

    For every input, ask when it is known. Anything known only after the outcome is out of the model.

  3. 3

    Re-run it honestly

    Rebuild with the fixes. A score that drops usually means the leak has left.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: information the model should not have gets in while it learns, so scores look too good.
  • The places it hides: preprocessing on all rows, late features, time-blind splits and linked rows.
  • The fix: split first, use pipelines, split by time or by group, and check each feature's timing.
  • Warning signs: a score that is too good to be true, one dominant feature, a gap to live results.
  • Leakage is a method error, separate from a privacy breach.
  • Honest about cost: a lower score that holds up beats a high one that fails in production.

Common traps

  • Trap"Our accuracy is 99 percent, so the model is ready."

    BetterA score that high is a warning. Check the split and the timing of each feature before you celebrate.

  • Trap"Data leakage is when someone steals our data."

    BetterThat is a privacy breach. Data leakage here is a modelling error that inflates the score.

  • Trap"Scale everything first, then split. It is fine."

    BetterThe scaler then sees the test rows. Split first, and fit the scaling inside a pipeline.

Can you spot the leak?

Spot the leak

Which snippet leaks test data into training?

Quick check

A model predicts loan default at application time. Which feature must go?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q422Train/validation/test splits?11 min
  2. Q423What is cross-validation?13 min
  3. Q413Overfitting vs underfitting?10 min
  4. Q412Bias vs variance?12 min
  5. Q436How do you tune hyperparameters?10 min

Learn it in depth

Last updated on 27 November 2026

Building a model that has to work after launch?

I design and build AI products and the systems behind them, including how the data is split and checked. Book a short call and tell me what you are building.