What is data leakage in machine learning?
Data leakage is the use, in training or validation, of information the model would not have at the moment it has to predict something for real — the classic case is computing a mean, a standard deviation or an encoding over the entire dataset before splitting into train and test. The symptom is an inflated metric that collapses when the model meets new data. Prevention is organisational: split before you transform, and keep every preprocessing step inside the pipeline.
What does data leakage look like in practice?
Leakage happens when information that exists only because the whole dataset was assembled at once — and would not be available at prediction time — enters training or validation. Scaling a column with the mean of every row and only then splitting into train and test is the most repeated example: that mean already contains pieces of the test set.
It is also leakage to build a feature out of the target value, to leave future records in the training set of a time-ordered problem, or to have the same record on both sides of the split through duplicates or an ignored group.
The result is a model that looks great in validation and disappoints in production. Most of the time the algorithm is not the culprit; the data flow built before it is.
Which classic leakage examples show up in real projects?
The cases below repeat across tutorials, notebooks and competitions, and they often go unnoticed because the code runs without an error:
- Standardising or normalising the full dataset and splitting into train and test afterwards, carrying test statistics into the training set.
- Filling missing values with the mean of the entire column instead of the mean computed on the training data only.
- Building a feature from the target, such as target encoding calculated over every record or a column derived from the outcome.
- Splitting a time-ordered problem at random: the test set receives earlier records and the training set receives later ones.
- Choosing the best features while looking at the whole dataset and only then running validation — the choice has already seen the test set.
- Leaving duplicates, or records from the same customer and the same patient, on opposite sides of the split.
- Repeating feature engineering after seeing the validation metric, tuning until the number goes up.
Why does leakage inflate the metric and then disappear in production?
When information from the target or from the future enters training, the model finds shortcuts. It does not learn the pattern that will exist later; it learns a hint that is only there because the data was prepared with the full set.
In validation that hint is still present, so the metric rises. In production it does not exist, and performance drops. The gap between the notebook number and the real result is usually the first warning.
In temporal problems and in problems with groups — same customer, same patient, same store — leakage is quieter, because the random split looks correct and is not.
How do you prevent data leakage in day-to-day work?
The practical rule is split before you transform. Every step that learns something from the data — imputation, scaling, encoding, feature selection — should be fitted on the training data only and applied to the test data. In practice that means putting preprocessing inside a pipeline and validating the whole pipeline, not the model on its own.
It is worth building the habit of asking, for every column and every transformation: would this value exist at the moment of prediction?
- Separate train and test before computing any statistic.
- Fit the scaler, the imputation and the encoding on the training data and apply them to the test data.
- Run cross-validation refitting the preprocessing at every fold.
- Use a temporal split on data with time and a grouped split on data with groups.
- Check duplicates and ID overlap before trusting the metric.
- Drop features that only exist after the event you are trying to predict.
Where does this topic appear in the Python for Data Science and Analytics course?
The course covers exactly the points where leakage is born: data cleaning and preparation, outlier detection, feature selection and cross-validation. There are three main modules — Basic, Intermediate and Advanced — plus bonus modules, with guided practice exercises.
Machine learning is covered through linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost and LightGBM. The course closes with a final project: end-to-end exploratory analysis and machine learning.
Lessons are recorded and watched at the student's own pace, there are real market projects to build a portfolio, and access is unlimited with no expiry date. A certificate of completion and an exclusive community for networking are included. No prior programming knowledge is required to start; the course begins with IDE and Jupyter Notebook setup. Payment goes through the Hotmart checkout, by card or boleto — R$ 470, or 12 instalments of R$ 20,37 on a card. The course site is python.paanalytics.net.
What is the one-line rule to remember?
Split first, transform later. Every statistic used to prepare a column belongs to the training data, and the test set should behave as if it had arrived from the future — unseen until the moment of evaluation.
If a validation score looks surprisingly good, check the order of the steps before celebrating the model. Leakage is a data organisation mistake, and it is fixed by reorganising the flow, not by changing the algorithm.
See the Python for Data Science and Analytics course
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
Is data leakage the same thing as overfitting?
No. Overfitting is a model that fits the training data too closely, including its noise. Leakage is information that should not be available during training or validation at all. They can produce a similar symptom — a metric that looks better than reality — but the fix is different: overfitting is addressed with regularisation, simpler models and more data, while leakage is addressed by fixing the order of the data preparation steps.
Doesn't cross-validation already protect against leakage?
Only if every transformation is refitted inside each fold. If you scale or impute over the whole dataset and then run cross-validation, the folds still carry test statistics. Making the split the first step, and keeping preprocessing inside it, is what removes the leak.
How do I spot leakage in a notebook someone else wrote?
Read the order of the cells. If a scaler, an imputer, an encoder or a feature-selection step is fitted before the train/test split, or on a variable built from the target, that is leakage. Then look at duplicates and at ID columns to see whether the same entity appears on both sides of the split.
Is leakage only a problem for time-series data?
No. Time-ordered problems make it more visible, but the same issue appears with any grouped data — the same customer, patient or store split across train and test — and with any statistic computed over the full dataset before the split.
Do I need prior programming experience to follow the course?
No. The course starts from scratch and covers IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, data visualisation, data cleaning and preparation, and outlier detection before moving into machine learning. Lessons are recorded and watched at your own pace, access has no expiry date, and a certificate of completion is issued.