Data leakage

Data leakage is when a machine learning model is built or tested with information it would not have when making real predictions, so its measured accuracy looks better than its real-world accuracy.

A model is supposed to be judged on data it has never seen, using only the information it would have at the moment it makes a prediction. Leakage breaks that rule. The scikit-learn documentation defines it as using information during model building that would not be available at prediction time. The result is a test score that is too optimistic and a model that does worse once it meets genuinely new data in production.

Leakage comes in two main forms. In target leakage, an input is really a stand-in for the answer. Google's machine learning glossary gives the example of a model predicting whether a customer will buy a product, where one input records whether the customer spoke to a customer agent; agents were only assigned after a purchase, so the model simply learns that shortcut. In train-test leakage, information crosses between the data used for training and the data used for testing: duplicate records in both, a random split of time-ordered data that lets the model train on the future, or several X-rays of the same patient spread across both sets so the model partly memorises patients.

The scikit-learn documentation shows how easily it happens. It builds a dataset of 200 samples with 10,000 random inputs and random answers, so no method should beat a coin toss. Selecting the 25 most useful inputs using all the data and then splitting it produces a test accuracy of 76 per cent. Doing the selection only on the training portion gives about 50 per cent, the honest answer.

This is not only a beginner's mistake. Kaufman, Rosset and Perlich gave leakage a formal treatment in their 2011 KDD conference paper Leakage in data mining: Formulation, detection, and avoidance. In 2022 Sayash Kapoor and Arvind Narayanan surveyed the literature and found 17 fields where leakage errors had been reported, affecting 329 papers, sometimes producing overly optimistic conclusions, and set out a taxonomy of eight types.

The defences are procedural. Split the data before doing anything else, fit every preprocessing step on the training portion only, split by time or by person when records are ordered or grouped, and treat any result that looks too good with suspicion. The phrase has a second meaning in security writing, where data leakage means sensitive information escaping an organisation, for example through a chatbot. That is a different problem with the same name.

Common questions

What is data leakage in machine learning?

It is when information that would not be available at prediction time gets into model training or evaluation. The model appears to perform well in testing because it has, in effect, seen the answers. Its performance then drops on real, new data.

How do you prevent data leakage?

Split the data into training and test sets first, and fit any scaling, feature selection or other preprocessing only on the training set. Libraries such as scikit-learn provide pipelines that do this automatically, including inside cross-validation. For time-ordered or grouped data, split by date or by group rather than at random.

What is the difference between data leakage and overfitting?

Overfitting is when a model learns noise in its training data and does badly on new data; a clean test set will reveal it. Leakage corrupts the test itself, so the test score is inflated too. That makes leakage harder to spot, because the usual check is the thing that has been compromised.

Is data leakage the same as a data breach?

No. In machine learning, data leakage is an evaluation error that makes a model look better than it is. In security, the same words describe confidential data escaping an organisation. Context usually makes clear which is meant.

Finding more

Search for "data leakage machine learning", "target leakage" or "leakage reproducibility crisis". Library documentation such as scikit-learn's common pitfalls page gives worked examples, and the research literature on reproducibility in machine learning shows how often leakage reaches published papers. Be wary of any reported result that is far better than comparable work and does not describe how the data was split.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.