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

AI and LLM interview question Q424, Classic ML and deep-learning basics. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-data-leakage

## Say it in 30 seconds

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.

## Say it in 2 minutes

Data leakage is when information sneaks into a model while it learns, so its test score looks better than its real score. There are two common forms. One is test data leaking into training, for example when I scale every row before I split. The other is a feature that would not exist when we make a real prediction, like a column filled in after a loan has already defaulted.

Leakage hides in ordinary steps. A random split of three years of loans mixes the future into training. The same customer on both sides lets the model recognise the person, so it misses the wider pattern. Scikit-learn's own guide warns that test data used in feature selection will optimistically bias the model, and a 2022 arXiv preprint by Kapoor and Narayanan calls data leakage a widespread problem that has led to severe reproducibility failures.

How I catch it: a score that is too good to be true, one feature that dominates, or a big gap between validation and live results. How I fix it: split first, put every fitting step in a pipeline, split by time or by customer, and ask for each feature whether I would have it on the day of the prediction.

In an interview I would say that leakage is a method error that makes results look better than they are, and that the fix lives in the process, so a better model will not solve it.

## 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)

- **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?

*Diagram, Leaky and clean preparation: 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.*
1. The test rows now count toward the average before any split.
2. The model is built on that mixed data, so its score looks great.
3. The split comes first, so the test rows stay hidden from every step.
4. The scaler learns from training rows only, then just transforms the test rows.

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. **Split first:** Set the test rows aside before any cleaning, scaling or feature choice. The split is the first thing in the pipeline.
2. **Fit inside a pipeline:** Put scaling and feature selection in one pipeline, so each step learns only from the training rows.
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. **Keep linked rows together:** Keep every loan of one borrower, or every visit of one patient, on the same side of the split.
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:

```text
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. **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. **Time each feature:** For every input, ask when it is known. Anything known only after the outcome is out of the model.
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." Better: A 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." Better: That is a privacy breach. Data leakage here is a modelling error that inflates the score.
- Trap: "Scale everything first, then split. It is fine." Better: The 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?

A. scaler.fit(X_train), then scaler.transform(X_test)
B. X_all = StandardScaler().fit_transform(X), then split X_all into train and test
C. Split by application date, then fit the imputer on the training months only
D. Group loans by borrower, then split the groups into train and test

**Answer:** B. The scaler learns its average from every row, test rows included, before the split. Split first, then fit the scaler on training rows only, or put it in a pipeline.

**Quick check:** A model predicts loan default at application time. Which feature must go?

A. Requested loan amount
B. Monthly income stated on the application
C. Collections call logged after the first missed payment
D. Years of credit history at application

**Answer:** C. Collectors log that call only after a payment is missed, so the feature is known only after the outcome. The other three are known when the application arrives.

## Can you answer these from memory?

**Flash cards**

- **What is data leakage?** Information that should be unavailable gets into training, so scores look better than real results.
- **What is a common cause?** Fitting a scaler, imputer or selector on all rows before the split.
- **Why is a random split risky on time data?** Later months land in training, so the model learns from the future it is tested on.
- **How do you prevent leaks?** Split first, fit inside a pipeline, and split by time or by customer where rows link.
- **Is leakage a privacy breach?** No. Here it is a modelling error that makes the score look better than it is.
- **Is a feature filled in after the outcome safe?** No. Use only features you would have on the day of the prediction.

## Likely follow-up questions

- Q422 Train/validation/test splits? https://designervenkat.online/interview/ai/train-validation-test-splits
- Q423 What is cross-validation? https://designervenkat.online/interview/ai/what-is-cross-validation
- Q413 Overfitting vs underfitting? https://designervenkat.online/interview/ai/overfitting-vs-underfitting
- Q412 Bias vs variance? https://designervenkat.online/interview/ai/bias-vs-variance
- Q436 How do you tune hyperparameters? https://designervenkat.online/interview/ai/how-do-you-tune-hyperparameters

## Learn it in depth

- [Train - Test Data: splitting your data](https://designervenkat.online/learn/machine-learning/train-test-split)
- [Validation techniques: testing fairly](https://designervenkat.online/learn/machine-learning/validation-techniques)
