· Xiaojing Yang · Machine Learning · 2 min read

中文

Pipelines and Data Leakage

Why preprocessing belongs inside the validation pipeline, not before the split.

Core idea

Data leakage happens when information from evaluation data sneaks into training decisions.

1. The leakage problem

Data leakage can make a weak model look strong. The classic mistake is fitting preprocessing steps on the full dataset before splitting or cross-validation.

Safe pipeline
Raw data
Before learning anything
Split / CV fold
Separate train and validation
Fit preprocessing on train only
Scaler, vectorizer, imputer
Transform validation
Use train-fitted steps
Evaluate
No validation information leaked

2. Leakage examples

Leakage sourceWhy it is dangerous
Scaling before splitValidation distribution influences training transform
Feature selection before CVValidation labels guide features
Duplicate documentsModel sees near-test examples
Time leakageFuture information predicts past
Target leakageFeature directly encodes the label

3. sklearn example

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("clf", LogisticRegression(max_iter=1000)),
])
scores = cross_val_score(pipe, X, y, cv=5)

4. AI/NLP connection

In NLP, leakage can occur through deduplication failure, preprocessing vocabulary learned from all text, topic overlap, prompt examples that resemble test items, or benchmark contamination in LLM pretraining.

Pipeline mindset

Every learned preprocessing step belongs inside the training fold.

Research mindset

Every evaluation score needs a leakage audit.

Takeaway

Pipelines are not just cleaner code. They are evaluation protection.

Interview pattern

When this appears in an interview, I would answer in four layers:

  1. give the short definition;
  2. explain the intuition;
  3. name the common failure mode;
  4. connect it to a real evaluation or deployment decision.

References

Share:
Back to Blog

Related Posts

View All Posts »