You did StandardScaler(). then split into train/test. Why is this wrong?
easyAnswer
- The scaler was fit on the full dataset including the test set, so mean and std leak information from test data into training preprocessing.
- Effect: test metric is slightly optimistic, sometimes a lot on small data.
- Correct order: split first, fit on train only, transform train and test with the same fitted scaler.
- In practice: wrap everything in a Pipeline and pass it to or GridSearchCV — the split-then-fit invariant is automatic.
Check yourself — multiple choice
- It's fine as long as the metric is high
- The scaler sees the test set's mean/std — leakage. Split first, fit on train only
- It doesn't matter for tree models
- You should always transform before splitting
Fit on full data leaks test statistics. Split → fit-on-train → transform both sets.
#pipelines#data-quality#preprocessing
Practise Supervised Learning
215 interview questions in this topic.
Related questions
- Why should preprocessing live inside a scikit-learn Pipeline rather than being applied manually before?
- What does ColumnTransformer do and when do you need it?
- How do you handle class imbalance in a dataset?
- Which models need feature scaling and which don't?
- What is target leakage and how do you prevent it?
- Why must you standardize features before applying L1 or L2 regularization?