Repository navigation
FIX clamp negative variance to zero in r_regression - #33642
Closed
EdenRochmanSharabi wants to merge 3 commits into
Closed
EdenRochmanSharabi wants to merge 3 commits into
EdenRochmanSharabi wants to merge 3 commits into
Conversation
added 2 commits
March 26, 2026 21:20
When a feature column is constant, the variance computation (sum(x^2) - n * mean^2) can produce tiny negative values due to floating-point error. Taking the square root of these values triggers a RuntimeWarning and produces NaN instead of the expected zero. Clamp the intermediate squared norms to zero before taking the square root, which prevents the warning and correctly identifies constant features. Fixes scikit-learn#11395
Contributor
Author
|
Closing this for now. I'd like to discuss the approach on the issue first, as a maintainer previously suggested using mean_variance_axis which might be a better path. |
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.
Summary
Fixes #11395
r_regression(and by extensionf_regression) computes the scaled standarddeviation of each feature using the formula:
When a feature is constant, this should yield zero, but floating-point error
can produce tiny negative values (≈ -1e-16).
np.sqrtof a negative numbertriggers a
RuntimeWarningand returnsnan.Fix
Clamp the intermediate squared norms to zero before the square root:
This is a standard numerical safeguard for variance-via-moments computations.
The existing
force_finitelogic downstream then correctly handles theresulting zero norms (mapping them to F=0.0 and p=1.0).
Changes
sklearn/feature_selection/_univariate_selection.py: Clamp the varianceintermediate to zero before taking the square root.
sklearn/feature_selection/tests/test_feature_select.py: Addednon-regression test with a constant feature column that previously triggered
the warning.
Notes
A previous WIP PR (#21360) was closed; a maintainer suggested using
mean_variance_axisbut the minimal clamp approach is less invasive andpreserves sparse matrix support without changing the computation path.