Back to Evaluation, Leakage, and Unsupervised

Data Leakage — The #1 ML Bug

The reason your model is too good to be true is almost always leakage. Spot it before deployment. FIND_VIDEO: search 'data leakage machine learning examples' — recommended channel: Kaggle / StatQuest. Aim for 11 min or under.

9 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. ML ObjectiveThe goal of machine learning is training a model to predict accurately on unseen data.
  2. Defining LeakageData leakage occurs if the model trains on information that will not be available during production prediction.
  3. TF-IDF ExampleComputing TF-IDF on the entire corpus before splitting the data causes leakage.
  4. Standardization ExampleCalculating mean and standard deviation on the full dataset before splitting is a common leakage error.
  5. The SolutionTo avoid leakage, always split the data first, then apply transforms only on the training set.
  6. Cross-ValidationFeature processing must be done within each fold of cross-validation after the validation data is split.
  7. Duplicate RowsDuplicate rows between train and test sets, often from oversampling, can cause training example leakage.
  8. Group LeakageGroup leakage happens when related data points, like multiple X-rays from one patient, are split across train and test sets.
  9. Time LeakageIn time series data, mixing future data into the training set creates time leakage.
  10. Summary & ImpactLeakage causes the model to overestimate its utility, leading to failure when deployed in a production environment.
PDF notes

Frequently asked questions

Why does data leakage cause models to fail in production?

Leakage causes the model to partially memorize test data features, leading to unrealistically high performance scores during evaluation. In production, this memorized information is absent, causing performance to drop significantly.

How should I handle feature processing during cross-validation?

For every fold, the feature processing (like standardization) must be calculated only on the training data for that fold, and then applied to the validation data.

What is the primary rule for avoiding data leakage?

Always split the data into training and testing sets first, and only then perform any feature preprocessing or transformation steps.

What is 'time leakage'?

Time leakage occurs in time series data when future information is accidentally included in the training set, making prediction unrealistically easy.