If a training column contains the same value in every row, StandardScaler cannot give it unit variance. With the default settings, the scaler uses a scale factor of one for that column and subtracts its training mean. The transformed training values therefore become zero. This is expected behaviour, not automatically a broken preprocessing step.
The important follow-up question is what happens when a later observation differs from that training constant. The answer is not necessarily zero: the fitted scaler still subtracts the original mean and divides by one.
Separate the three fitted attributes
The official StandardScaler reference describes mean_, var_ and scale_. The first stores training means, the second training variances, and the third the factors used for division. Ordinarily scale_ is the square root of var_, but a zero-variance feature receives a factor of one rather than a zero divisor.
With default centering and scaling, the calculation is the feature value minus its fitted mean, divided by its fitted scale. Do not confuse scale_=1 with proof that the original column varied. Inspect var_ as well. A constant column has no training variation even though its division factor is a perfectly ordinary positive number.
Run a small constant-column experiment
Use a disposable Python environment containing NumPy and scikit-learn. The first column below varies, while the second stays at seven. These are illustrative numbers, not measurements from a deployed model.
import numpy as np
from sklearn.preprocessing import StandardScaler
train = np.array([[1.0, 7.0], [3.0, 7.0], [5.0, 7.0]])
scaler = StandardScaler()
scaled = scaler.fit_transform(train)
later = scaler.transform([[3.0, 9.0]])
print("mean:", scaler.mean_)
print("variance:", scaler.var_)
print("scale:", scaler.scale_)
print("training constant column:", scaled[:, 1])
print("later row:", later)
assert scaler.var_[1] == 0.0
assert scaler.scale_[1] == 1.0
assert np.allclose(scaled[:, 1], 0.0)
assert np.allclose(later, [[0.0, 2.0]])
assert np.allclose(scaler.inverse_transform(scaled), train)
The assertions express the intended behaviour without depending on printed spacing. The later second value is nine, so subtracting the fitted mean seven produces two. That number is not two training standard deviations: the training standard deviation for that feature was zero, and the implementation substituted a safe division factor.
Do not refit to make a new batch look normal
Keep the fitted training scaler when transforming validation, test or later operational rows. Fitting again on the later batch changes the reference statistics and can hide a change in the input distribution. Combining training and test data to calculate the scaler also allows test information into preprocessing.
For an end-to-end training workflow, the site’s imputer and pipeline tutorial explains the related separation between fitting preprocessing on training rows and applying it elsewhere. A pipeline helps keep these operations aligned, but does not make unsuitable data suddenly informative.
Decide whether the column belongs in the model
A constant training feature offers no observed within-training variation for learning its effect. Investigate why it is constant before deleting it: a filtered extract, incorrect join, measurement failure or legitimate fixed setting can produce the same appearance. Compare the column’s meaning and collection process with your training population.
If you remove the feature, make that a deliberate preprocessing decision fitted on training data and applied consistently later. Keep feature order and names aligned. Do not simply drop a column from one prediction batch while leaving the trained model expecting the original input shape.
StandardScaler is also sensitive to outliers, and scaling does not make an arbitrary distribution Gaussian. A training constant becoming variable later is a reason to review data quality or distribution change, not evidence that the model has learned how that variable affects the target.
Quick questions and review checklist
Does with_mean=False behave the same way? No. Without centering, a constant column is not shifted to zero; with a scale factor of one, its values remain unchanged. Sparse inputs require particular care because centering them would destroy sparsity and is rejected by this scaler.
Must every transformed training column have variance one? No. A truly constant column cannot acquire variation through this transformation. Check the fitted settings and the original data before treating that result as an error.
- Inspect mean_, var_ and scale_ together.
- Fit only on the intended training data.
- Test both an unchanged and a changed later value.
- Record any feature-removal decision with the model’s input schema.
This tutorial is for educational purposes. Test carefully before production use. The site owner is not responsible for misuse, damage, data loss or production issues.



