Fix StandardScaler discarding the mean on a constant feature - #363
Merged
josevalim merged 2 commits intoAug 26, 2026
Merged
Conversation
With a zero-variance feature, fit forced the stored mean to 0.0 instead of the real mean, so transform silently returned the raw (uncentered) values for that feature instead of 0.0. sklearn keeps the true mean and only forces scale_ to 1.0, so the constant feature transforms to 0.0 like every other case. scale/3 already divides by 1.0 when std is 0, so removing the forced-zero mean is enough to match.
Verified against sklearn.preprocessing.StandardScaler directly: it only clamps scale_ to 1 for a zero-variance feature, mean_ is always the real column mean. The strongest case is transform on data the scaler was not fit on - a constant training feature must still center new values by the fitted mean, not pass them through raw. Confirmed these tests fail against the code they replace and pass here.
Contributor
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StandardScalerzeroed out the mean for any zero-variance feature instead of keeping it, so a constant feature came out uncentered instead of 0.0. Worse on new data: transforming an unseen value through a constant training column just passes it through raw instead of centering it, which is silent data leakage into a pipeline that assumes standardized input.sklearn only clamps the scale to 1 for a constant feature, it never touches the mean. Removed the line that zeroed it, the existing division-by-1 guard was already enough.