· 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.
Before learning anything
Separate train and validation
Scaler, vectorizer, imputer
Use train-fitted steps
No validation information leaked
2. Leakage examples
| Leakage source | Why it is dangerous |
|---|---|
| Scaling before split | Validation distribution influences training transform |
| Feature selection before CV | Validation labels guide features |
| Duplicate documents | Model sees near-test examples |
| Time leakage | Future information predicts past |
| Target leakage | Feature 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:
- give the short definition;
- explain the intuition;
- name the common failure mode;
- connect it to a real evaluation or deployment decision.