2.1.13 · D1Data Preprocessing & Feature Engineering

Foundations — Data leakage identification and prevention

2,248 words10 min readBack to topic

Before we can talk about how leakage happens, we must agree on what every squiggle and word in the parent note means. This page assumes nothing. If you have never seen a summation sign or the letter , start here at line one.


The cast of characters (built one at a time)

1. A dataset: rows and columns

Picture a spreadsheet. Every row is one thing you observed (one transaction, one house, one day of the stock market). Every column is one measurement about that thing.

Figure — Data leakage identification and prevention

Why the topic needs this: leakage is entirely about which columns are allowed to be features. If a column secretly contains target information, disaster follows. So we must be crystal clear on the feature/target split first.


2. The symbols and

Instead of writing "the features" and "the target" in English every time, we use letters.

The picture: is the left block of the spreadsheet, is the single column on the right. The subscript points at a single row across both blocks.

Why: the whole topic is about the relationship between (what you're allowed to use) and (what you want). Leakage means was contaminated by knowledge of .


3. A model, written

A model is a machine that turns features into a guess for the target.

The picture: a box with an arrow in (the row ) and an arrow out (a number ).

Figure — Data leakage identification and prevention

Why the "hat": the hat always means "our estimate of", never the true value. is the truth; is the guess. Keeping them separate is the whole point of measuring error.


4. The summation sign

We need to add up something over every sample. Writing is tedious, so mathematicians invented shorthand.

The picture: a "for every row, grab this number and drop it in a bucket; at the end, report the bucket's total."

Why this tool and not another? We use (not, say, an integral) because a dataset is a finite list of separate rows — you add discrete things, you don't smoothly integrate. It answers the question "what is the total across my whole dataset?"


5. The loss — measuring wrongness

We need a number that says how badly a single guess missed.

A common choice is the squared difference:

Why squared? Two reasons. First, squaring makes the penalty positive whether you guessed too high or too low (a miss of and a miss of both cost ). Second, squaring punishes big misses much harder than small ones — a miss of costs , a miss of costs only . That answers "how do I turn 'right or wrong' into a single comparable number?"

Figure — Data leakage identification and prevention

6. Empirical risk — average wrongness on YOUR data

One loss is for one sample. To score the whole model, average the loss over every sample you have.

Let's read this left to right, out loud:

  • WHAT: — go through every sample to .
  • For each, compute — how wrong the guess was versus truth .
  • — divide the total by to get the average wrongness per sample.

Why the hat on ? Same rule as before: it is our estimate of true error, computed only from the data we happen to hold. It is not the truth about how the model behaves everywhere.


7. Expectation and true risk — wrongness in the whole world

Your dataset is a small sample of reality. What you really care about is the average error over all possible future data, not just the rows you own.

Why we need both and : the entire disaster of leakage is that (what you measure on your data) becomes tiny while (what happens in the real world) stays huge. The gap between the hat-version and the true-version is the villain of this whole topic.


8. A probability distribution

The picture: imagine a cloud of dots, one per possible pair; denser regions are more likely. Three clouds — train, test, production — that we hope are the same shape.

Why this is the keystone: machine learning only works if the past resembles the future in shape. Leakage secretly deforms (by adding columns that won't exist later) so it no longer matches . Every type of leakage is a specific way this equation gets broken. See Train-testvalidation split strategies for how we build the test cloud honestly.


9. Function composition and the leak function

The parent note writes features as . Let's unpack that.

The picture: two boxes in a row. If box has a hidden wire feeding it the answer , then box downstream is effectively being handed the answer. That's target leakage in one image.

Why: this notation lets us say precisely where the leak lives — inside , before the model ever runs. See Causal inference basics for why the direction of the wire (does cause the feature, or vice versa?) decides guilt.


10. Greek helpers: , , ,

These appear in the scaling and target-leakage derivations.

The scaling formula from the parent note:

Reading it: is the average (there's our and again). averages the squared distances from the mean (there's squaring, to keep distances positive). The last line rescales each value: subtract the mean so the column centres on zero, divide by the spread so it has a standard size.

Why the topic needs and : scaling leakage happens when you compute and using the test rows too — the test cloud leaks into the recipe. More on the honest procedure in Feature scaling and normalization.

Why : the parent writes — the gap between real-world error and measured error. The bars mean absolute value (drop the minus sign, keep the size). Leakage makes large.


How these feed the topic

rows and columns

features X and target y

model f gives y-hat

loss L measures one miss

sum averages into risk R-hat

expectation E gives true risk R

distributions P train test prod

the ML assumption they match

leak recipe g uses y or future

composition f of g

Data Leakage the gap between R-hat and R

mean mu and spread sigma

Everything funnels into the parent topic Data leakage: leakage is precisely the situation where the honest chain (rows → features → model → risk) is corrupted by a recipe that used or the future, so measured risk no longer predicts true risk .


Worked mini-check: reading a formula aloud


Recall drills

Recall What does the hat mean in

and ? "Our estimate of" — is the guessed target, is measured (empirical) risk from our data, not the true world value.

Recall In one sentence, what is data leakage in

/ language? A feature column in was built with knowledge of (or the future), information that won't exist at prediction time.

Recall Why does leakage make

large? Because drops near zero on the contaminated training data while (true production error) stays high, so their gap blows up.


Equipment checklist

Read the left side; say the answer before revealing.

I can explain what a sample, feature, and target are
A sample is one row/observation, a feature is an input column, the target is the column we predict.
I know what , , , mean
= all features, = target; the subscript selects one specific row.
I can read
The model turns row 's features into a guess (y-hat) for its target.
I can expand
— add the term for every sample from 1 to n.
I know why loss is often squared
Squaring keeps errors positive and punishes big misses far more than small ones.
I can compute empirical risk
Average the per-sample loss: .
I know the difference between and
is measured on my finite data; is the true long-run average over all real-world data.
I understand
The core ML assumption that training, test, and deployment data share the same shape/distribution.
I can read and say what is
Apply then ; is the feature-building recipe, and it leaks if it secretly uses or future info.
I know what , , , stand for
Mean, standard deviation (spread), a tiny quantity/noise, and a difference/gap.