Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
AI & Machine Learning

Data Leakage: The Silent Bug That Makes Models Look Brilliant

Last updated:

The result that should worry you

A supplier sends over a churn model with 97% accuracy. The board is delighted. Three months after launch, it is barely better than guessing. Nothing in the code is broken, nothing crashed, and no error was logged.

This is the classic signature of data leakage. The model learned from information that was available in the historical data but will never be available when a real prediction is needed. In testing it was effectively allowed to peek at the answer.

Leakage is the most common serious bug we find when reviewing other people's models, and it is almost never deliberate. It creeps in because historical data is a snapshot of everything that eventually happened, and the model cannot tell the difference between cause and aftermath.

A worked example

Take the churn model above. The training data is one row per customer, with a column saying whether they cancelled. Among the inputs is 'number of calls to the retention team' and 'account status notes'.

Customers who cancelled were, almost by definition, passed to the retention team, and their notes say things like 'requested cancellation'. The model learns that retention calls predict churn. Perfectly true, and perfectly useless: by the time a retention call happens, the customer has already decided to leave. At the moment you would want to intervene, that feature is zero for everyone.

For every input, ask one question: on the day we would make this prediction, would we actually know this value?

The common forms of leakage

  • Target leakage. An input that is a consequence of the outcome, like the retention calls above, or a 'write-off reason' in a late-payment model.
  • Time leakage. Using values calculated with future data, such as a customer's total annual spend when predicting something in March.
  • Duplicate leakage. Near-identical records, such as the same invoice exported twice, landing in both training and test sets.
  • Group leakage. Several records from the same customer, patient or machine split across training and test, so the model recognises the individual rather than the pattern.
  • Preprocessing leakage. Scaling, filling missing values or selecting features using statistics from the whole dataset, including the test portion.
  • Overwritten fields. CRM or ERP fields that hold today's value rather than the value at the time, like a customer segment updated after they churned.

How to spot it

  1. Distrust surprising accuracy. If a business prediction scores far better than experienced staff manage, investigate before celebrating.
  2. Look at feature importance. One input dominating everything is a warning sign, especially one recorded late in a process.
  3. Walk through a single real case. Pick one customer, go to the prediction date, and check which inputs actually existed then.
  4. Check when each field is populated. Ask the people who use the system, not just the data dictionary.
  5. Test on a later period. Train on older data and test on the most recent months. A large drop compared with random splitting often points to leakage.

Fixes for each type

LeakTypical symptomFix
Consequence of the outcome used as inputOne dominant feature, near-perfect scoresRemove it, or use only its value before the prediction date
Future values in aggregatesGreat backtest, poor live resultsCalculate features as at the prediction date
Same entity in train and testPerformance drops sharply for new customersSplit by customer, site or machine
Preprocessing on full datasetSmall but consistent optimismFit preprocessing on training data only, inside a pipeline
Overwritten system fieldsModel fails after launch without clear causeUse change history or build snapshots going forward

The overwritten-field problem is the hardest, because the historical truth may simply be gone. When that happens we either find an audit log, or start saving dated snapshots and accept that a proper model has to wait for enough history to build up.

Building leakage out of the process

Most leakage is prevented by structure rather than vigilance. Build training data with point-in-time joins, attaching to each historical event only the values that existed at that timestamp. Keep all preprocessing inside a single pipeline object fitted on training data. Split by time and by entity as the default, and only use random splits when there is a good reason.

Keeping feature calculations in one shared place, whether a simple set of tables or a full feature store, makes point-in-time correctness far easier. Leakage also appears in forecasting when promotions or stock levels are known only in hindsight, which we touch on in forecasting demand with your own data.

A second pair of eyes is cheap

Leakage is easy to miss when you built the model yourself, because you know what every field is supposed to mean. At SpiderHunts, every model gets a review from someone who did not build it, with the explicit job of asking 'would we have known this?' for every input. It takes a few hours and has saved more than one project from a painful launch.

If you have a model that performs suspiciously well, or one that fell apart after going live, our data science team can review it. Usually the fix is straightforward once the leak is found. The realistic accuracy is lower, but it is accuracy you will actually get.

Frequently asked questions

What is data leakage in machine learning?

Data leakage is when a model is trained on information that would not be available at the time of a real prediction. It makes test results look far better than production performance will be.

How do I know if my model has data leakage?

Warning signs include accuracy that seems too good, one feature dominating the model, and a large drop in performance when tested on a later time period. Walking through real cases to check when each input was known is the most reliable check.

What is target leakage?

Target leakage is a specific kind where an input is a result of the outcome you are predicting, such as using a cancellation note to predict cancellation. The model learns the aftermath rather than the cause.

Should I use a random or time-based train-test split?

For most business predictions, use a time-based split, training on earlier data and testing on later data, because that mirrors how the model will be used. Also make sure the same customer or entity does not appear on both sides.

Can data leakage be fixed after a model is built?

Yes. Remove or correct the leaking inputs, rebuild the training data with values as they were at prediction time, and retrain. Expect the measured accuracy to drop, because the original figure was never real.

Keep reading

Model results that look too good to be true?

They may be. Share how the model was built and tested, and we will review it for leakage and tell you what accuracy you can realistically expect in production.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

AI AgentsMachine LearningAI Integration