Transform and fit_transform
Quick answer
Transformers convert features into a new representation. Fit learns any required statistics, transform applies them, and fit_transform combines the two for training. Use fit_transform on training data and transform on validation or test data, retaining the same fitted transformer. All library examples are standalone local Python examples tested with scikit-learn 1.8.0; ByteMind package execution is not verified.
Freeze the training mean and scale
from sklearn.preprocessing import StandardScalers = StandardScaler()print(s.fit_transform([[2], [6]]).tolist())print(s.transform([[10], [0]]).tolist())print(s.mean_.tolist())Expected output:
[[-1.0], [1.0]][[3.0], [-2.0]][4.0]The held-out rows use mean 4 and scale 2; the fitted mean is unchanged.
How it works
StandardScaler learns a mean and scale per feature and applies (x - mean) / scale. The default scale is based on population variance (ddof=0); a constant column receives scale 1. For ordinary transformers fit_transform can be equivalent to fit then transform and may be implemented more efficiently. Some supervised encoders intentionally behave differently: TargetEncoder.fit_transform uses internal cross-fitting on training rows, whereas fit followed by transform uses statistics from the full fitted training set. Preserve the representation expected by the downstream estimator. A transform can change the number of columns, but a normal tabular transformer keeps samples aligned with targets. Use get_feature_names_out where the transformer supports it. Do not assume sparse and dense outputs have identical memory costs.
Check constant feature and output shape
from sklearn.preprocessing import StandardScalers = StandardScaler().fit([[5, 0], [5, 4]])print(s.scale_.tolist())print(s.transform([[5, 6]]).tolist())Expected output:
[1.0, 2.0][[0.0, 2.0]]Scale 1 prevents division by zero in the constant feature.
Common mistakes
Fitting a scaler on validation data changes its center and scale and lets information from the validation distribution influence evaluation. Do not refit on one incoming row: it can collapse to zero after standardization. Never center a sparse matrix with StandardScaler(with_mean=True); use an appropriate sparse-compatible strategy.
Practice
Train a center-only scaler on [2, 6]. Transform [10, 0] using its frozen mean of 4. Explain why fitting again on [10, 0] would yield different results.
Reference candidate: concept-transform-and-fit-transform. Review the associated knowledge check. Native estimator tasks require a local scientific Python environment.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Transform and fit_transform — knowledge check
Apply estimator contracts and leakage-resistant pipelines to small fixtures.
Which call should apply a fitted scaler to validation rows?
Training mean is 4 and scale is 2. What is the transformed value for 10?
Which transformer can deliberately differ between fit_transform and fit then transform?
What is the default scale assigned to a constant feature by StandardScaler?
Why preserve a fitted transformer for serving?