M2-080M2: Classical Machine LearningMissing Values & Data LeakageHard
⚡Flashcard Drill➔Mastery:
Why Global Normalization Before Splitting Causes Data Leakage and How to Fix It
🎯 Active Recall Self-Check (Draft First, Verify After)
Login-free practice tracker: recall core answer, audit pitfalls, and log review status.
Step 1 · Draft your concise answer & assumptions
Step 2 · Check Pitfalls & Follow-up Inquiries
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Global standardization uses test set mean and variance to scale training data, artificially shrinking generalization error; fix by fitting scalers strictly on training folds.
📌 Key Takeaways
- •Mechanism: computing and incorporates test distribution information into training inputs
- •Consequence: optimistic evaluation bias, masking true out-of-distribution generalization failure
- •Fix: encapsulate transformations in Pipelines, fitting only on training data and transforming test data
📐 Mathematical Derivations
Mathematical Formulation: Let dataset . Global standardization computes: , .
When training samples are standardized via , their values depend directly on . This violates the fundamental statistical premise of supervised learning: must be an independent mapping of alone. If has a shifted mean , global normalization partially aligns training features with the test domain, artificially inflating cross-validation performance.
When training samples are standardized via , their values depend directly on . This violates the fundamental statistical premise of supervised learning: must be an independent mapping of alone. If has a shifted mean , global normalization partially aligns training features with the test domain, artificially inflating cross-validation performance.
🏭 Production Trade-offs
Correct Implementation Pattern:
```python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
# Pipeline guarantees scaler.fit() is executed strictly on training folds
pipe = Pipeline([
('scaler', StandardScaler()),
('model', LogisticRegression())
])
pipe.fit(X_train, y_train)
During inference, use
```python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
# Pipeline guarantees scaler.fit() is executed strictly on training folds
pipe = Pipeline([
('scaler', StandardScaler()),
('model', LogisticRegression())
])
pipe.fit(X_train, y_train)
`During inference, use
scaler.transform(X_test) using the frozen parameters .⚠️ Common Interview Pitfalls
- ✕Calling
scaler.fit_transform(X)on the complete dataset before callingtrain_test_split - ✕Calling
scaler.fit_transform(X_test)on the test set instead ofscaler.transform(X_test)
🎯 Interviewer Follow-up Inquiries
- ?Why is the performance inflation from global normalization worse when sample sizes are small?
- ?What other preprocessing steps are prone to this exact same leakage failure mode?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚