Skip to main content

What is data leakage, and how do you prevent it?

Machine learning basics · Intermediate 2

Short answer

Leakage is when information that would not be available at prediction time gets into training, so validation scores look great but production disappoints. Causes include features computed from the future or the target, and preprocessing fitted on the whole dataset. Prevent it by splitting first, fitting every transform inside the training fold, and splitting by time or group where needed.

Deeper answer

Classic examples: predicting loan default with a feature like "number of collection calls", which only exists after default; scaling or imputing with statistics from the full dataset before splitting; or random splits on time-series data so the model sees the future.

The defence is mostly discipline. Split before any exploration that drives modelling choices. Put preprocessing in a pipeline so cross-validation refits it on each training fold. Use time-based splits for anything that changes over time, and group splits when the same customer or patient appears in many rows. A suspiciously high score is itself a warning sign worth investigating.

What a strong answer covers

Answer it out loud in the mock interview about 1.5 min

Follow-ups to expect

Common mistakes

Learn it

Cross-validation and leakage

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.