Fitting Scalers on Training Data Only
What You'll Learn
You'll understand why you must fit scaling transformations only on training data to avoid leaking information from your test set.
The Core Problem: Data Leakage
When you scale features, the scaler learns parameters from your data—like the minimum and maximum for Min-Max normalization, or the mean and standard deviation for standardization. If you calculate these parameters using your entire dataset (training + test), you're cheating.
Here's why: in real-world deployment, your model will never see the test data beforehand. By including test data when fitting the scaler, you're giving your model a sneak peek at information it shouldn't have. This leads to overly optimistic performance estimates during evaluation.
The Correct Workflow
Think of it like studying for an exam: you learn the material (fit the scaler) from your textbook and notes (training data), then apply what you learned to the actual test (transform test data with the fitted scaler). You don't get to see the test questions while studying!
The proper steps:
- Fit the scaler on training data only:
scaler.fit(X_train) - Transform the training data:
X_train_scaled = scaler.transform(X_train) - Transform the test data using the same fitted scaler:
X_test_scaled = scaler.transform(X_test)
Notice that you never call fit() or fit_transform() on test data—only transform() with parameters learned from training data.
Why This Matters
If test data influences scaling parameters, your model's test performance won't reflect how it will perform on truly unseen data in production.
Key Takeaway: Always fit scalers exclusively on training data, then apply the same transformation to test data—this prevents data leakage and ensures honest performance evaluation.