Feature scaling: fit on training data only
In this article (3 sections)
Standardization estimates a mean and scale. If those statistics include validation or test rows, the transformation has already learned from the evaluation population. Targets are unnecessary for this leakage.
Make the leak measurable
Our construction has 200 training rows and 100 test rows with two features. Training feature zero has mean 0.0259; the shifted test mean is 4.9213.
A proper scaler fitted on training stores 0.0259. Transforming test produces mean 4.5189 on that standardized coordinate, revealing substantial drift. A leaky scaler fitted on all 300 rows stores 1.6577 and makes transformed test mean look smaller at 1.2863.
The smaller number does not mean the test population became more compatible. The scaler moved toward it because it was allowed to inspect it.
The StandardScaler documentation defines training-sample statistics stored for later transform calls.
import numpy as np
from feature_cases import scaling_case
r = scaling_case()
assert np.allclose(r['proper_mean'], r['train_mean'])
assert not np.allclose(r['leaky_mean'], r['train_mean'])
assert r['proper_test_transformed_mean'][0] > 4
assert r['leaky_test_transformed_mean'][0] < 2
print(r)Run the case in the feature-engineering lab. It isolates a preprocessing boundary without fitting a predictive model.
Fit transformations inside every fold
Use a pipeline so imputation, scaling, feature selection and modelling fit on each cross-validation training fold. Transform the fold's validation rows with those stored objects. After selection, refit the complete pipeline on permitted development data and evaluate test once.
Not every model requires scaling, but consistent units still affect distances, penalties, kernels and optimization. RobustScaler or nonlinear transformations make different assumptions and need the same split discipline.
At serving, load the fitted scaler rather than recomputing statistics on a request batch. Batch-specific normalization makes one prediction depend on which other requests happen to arrive. Validate feature order, units and finite values before transformation.
Monitor raw feature distributions and transformed values. Large standardized values can reveal drift, legitimate extremes or unit errors. Do not silently refit to hide them.
Exercise: place scaling and logistic regression in a pipeline, compare proper cross-validation with pre-split global scaling, and send an input expressed in paise instead of rupees. Write the unit contract that catches the error before scaling.
NeuraPath's Data Science course treats scalers as learned model artifacts. Their fit boundary and stored units belong in the evidence package.
Continue learning
This article is part of the Feature engineering and data quality sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in One-hot encoding with unseen categories at inference.
- Continue with Missing-value indicators: when missingness carries information.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science