Scaling and missing values.
Learn which transformations depend on training data and why missing is not the same as zero.
Start with the meaning.
Real tables have columns in different units. Age may range from 18 to 90 while annual income ranges into thousands. A distance-based model can let the larger numeric scale dominate. Standard scaling learns a training-column mean and standard deviation, then transforms each value by subtracting the mean and dividing by the spread. A transformed value near zero is near the training mean; one near 1 is about one training standard deviation above it. This changes representation, not the underlying person's facts.
Missing values require a separate decision. A blank measurement is not automatically zero: zero bedrooms and unknown bedrooms mean different things. Imputation fills missing entries according to a rule, often the median of observed training values for a numeric column. The median is less affected than the mean by extreme values, but neither can recover information that was never recorded. A missingness indicator can preserve the fact that a value was absent when that absence itself may be informative.
How the idea works.
A transformer that learns statistics must fit on training rows only. If a scaler computes a mean from all rows before splitting, test data influences the learned transformation. Even if target labels are never used, that is still information leakage. The proper order is split first, fit the transformation on training rows, and apply that fitted transformation unchanged to validation, test, and later live inputs. A pipeline packages this order.
Not every model needs the same preprocessing. KNN and many linear or kernel methods are sensitive to numeric scale. Trees split on ordering and usually care less about standard scaling. Missing-value handling varies by estimator and library version. An imputation rule should be selected for the feature's meaning and the model's requirements, not copied blindly from another project.
Missingness can be systematic. A measurement may be absent because the instrument was unavailable, because a person declined to answer, or because the field was introduced late. Those causes may differ across groups and over time. Filling every blank with one number can hide them. Document how many values are missing, where, and whether the missingness mechanism may change at prediction time.
Follow the code.
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
X_train = np.array([[10.0], [20.0], [np.nan], [30.0]])
X_new = np.array([[np.nan], [25.0]])
prepare = make_pipeline(
SimpleImputer(strategy="median"), StandardScaler()
)
prepare.fit(X_train)
print(prepare.transform(X_new))np.nan marks a missing numeric entry in this array. SimpleImputer learns the median of the observed training values 10, 20, and 30, which is 20. It replaces the missing training value and the missing new value with 20. StandardScaler then learns its mean and spread from the imputed training rows. transform applies both learned steps to X_new, returning two scaled rows. The new value 25 influences only its own transformed output; it does not change the training median or scaler statistics.
This code shows preprocessing alone, without a predictor. A complete pipeline would append a model as the final step. The two-step order matters: the scaler must see filled numeric values. A different feature, such as a category name, would need a different imputation and encoding path.
What the result does not say.
Scaling can make coefficients more comparable numerically, but it does not make them causal or eliminate all model assumptions. A standard deviation can be zero for a constant feature; library transformers handle such edge cases according to their contracts, while a hand-written formula needs its own rule.
A live system must use the exact transformation learned with the model. Recomputing a mean from one new input or a recent batch changes what the model receives and can silently alter predictions. The preprocessing is part of the fitted model artifact.
One clear idea.
Fit data-dependent preparation on training rows and apply the same learned transformation everywhere else.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.