← Back to Classic ML Mind Map
📊 Classic MLID: data-leakage

Data Leakage

数据泄漏
🎯Core Definition
Data leakage means the training process uses information unavailable at validation/test (or production) time; three types: ① target leakage — features contain the target directly or indirectly (using “already defaulted” to predict default, refund amount to predict refunds); ② temporal leakage — predicting the past with future information (predicting tomorrow's price with today-plus-1 features); ③ pipeline leakage — preprocessing fit on the whole dataset before splitting (scaling, imputation, feature selection, target-encoding statistics have all “seen” the validation folds). Consequences: inflated train and CV scores, abnormal feature importances, and a cliff in production. Three iron rules: scaling/imputation/encoding must be done inside each CV fold (fit on the train fold, transform the validation fold); every derived statistic (means, encodings, selected features) is computed on the train fold only; time-series data is split by time before any processing.
💡Use Cases
a universal risk in every modeling pipeline; interviews ask “scaling inside or outside CV”, “what does OOF encoding prevent”, “how to detect leakage”; a classic failure point in competitions and industrial risk control.
⚡Key Problems Solved
a “perfect” high-scoring model may have learned no generalizable pattern and fails the moment it ships; encapsulating preprocessing inside the CV pipeline (e.g. sklearn Pipeline fit/transform per fold) keeps validation scores aligned with production — OOF encoding and in-CV scaling exist precisely to close the last invisible leak so offline evaluation is an honest proxy for online performance.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Data Leakage.
Use Cases
In production engineering, what are the primary use cases for Data Leakage and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Give a concrete example of each leakage type (target/temporal/pipeline); why do all three inflate CV scores?
2
Why must scaling happen inside CV (split first, then fit the scaler)? What goes wrong with fit-on-all-then-split?
3
How do target encoding and group-mean statistics leak? Which leakage does the OOF procedure prevent?
4
How do you detect leakage (near-zero training error, CV far below training, abnormal importances, AUC near 1)?
5
What forms can future information take in time-series? How do lag features and rolling statistics leak when misused?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Data Leakage"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardTarget EncodingNext Card →SHAP & LIME

🔗 More Classic ML Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/ML/card/data-leakage
中文速览与面试要点

数据泄漏 (Data Leakage)

定义: 数据泄漏指训练过程用到了验证/测试阶段(线上)拿不到的信息, 分三类: ① 目标泄漏 (target leakage): 特征里直接或间接包含目标信息——如把“是否已违约”当特征预测违约、把退款金额当特征预测退款; ② 时序泄漏 (temporal leakage): 用未来信息预测过去——如预测明天股价, 特征却含明天的开盘价; ③ 管线泄漏 (pipeline leakage): 预处理在 CV 切分之前对整个数据集 fit——标准化、缺失值填充、特征选择、目标编码的统计量都“见过”验证折。后果: 训练与 CV 分数双高、特征重要性异常、上线后性能断崖。三条铁律: 标准化/填充/编码必须在每折 CV 内部完成(只 fit 训练折、transform 验证折); 任何由数据衍生的统计量(均值、编码、选择出的特征)只用训练折计算; 时序数据先按时间切分再处理。 使用场景: 所有建模流程的通用风险, 面试常问“标准化在 CV 内部还是外部”“OOF 编码防什么泄漏”“怎么发现泄漏”; 竞赛与工业风控中的经典失分点。 解决的问题: “看似完美”的高分模型其实没有学到泛化规律, 一旦上线立刻失效; 把预处理封装进 CV 流程(如 sklearn Pipeline 在每折内 fit/transform)能保证验证分数与线上一致——OOF 编码、CV 内标准化都是为了消灭这“最后一个看不见的泄漏口”, 让离线评估成为线上表现的诚实代理。

核心要点

  • 三类泄漏(目标/时序/管线)各举一个具体例子, 为什么它们都会让 CV 分数虚高?
  • 标准化为什么必须在 CV 内部做(先 split 再 fit scaler)? 先 fit 全量再 split 会发生什么?
  • 目标编码/类别均值统计如何泄漏? OOF 编码的具体步骤是防哪种泄漏?
  • 如何检测泄漏(训练误差极低、CV 远低于训练、特征重要性异常、AUC 接近 1)?
  • 时序数据中“未来信息”有哪些表现形式? 滞后特征/滚动统计做错了怎么泄漏?