Skip to content

FIX clamp negative variance to zero in r_regression - #33642

Closed
EdenRochmanSharabi wants to merge 3 commits into
scikit-learn:mainfrom
EdenRochmanSharabi:fix/f-regression-negative-sqrt
Closed

EdenRochmanSharabi wants to merge 3 commits into
scikit-learn:mainfrom
EdenRochmanSharabi:fix/f-regression-negative-sqrt

Conversation

@EdenRochmanSharabi

Copy link
Copy Markdown
Contributor

Summary

Fixes #11395

r_regression (and by extension f_regression) computes the scaled standard
deviation of each feature using the formula:

X_norms = np.sqrt(row_norms(X.T, squared=True) - n_samples * X_means**2)

When a feature is constant, this should yield zero, but floating-point error
can produce tiny negative values (≈ -1e-16). np.sqrt of a negative number
triggers a RuntimeWarning and returns nan.

Fix

Clamp the intermediate squared norms to zero before the square root:

X_norms_squared = row_norms(X.T, squared=True) - n_samples * X_means**2
np.maximum(X_norms_squared, 0, out=X_norms_squared)
X_norms = np.sqrt(X_norms_squared)

This is a standard numerical safeguard for variance-via-moments computations.
The existing force_finite logic downstream then correctly handles the
resulting zero norms (mapping them to F=0.0 and p=1.0).

Changes

  • sklearn/feature_selection/_univariate_selection.py: Clamp the variance
    intermediate to zero before taking the square root.
  • sklearn/feature_selection/tests/test_feature_select.py: Added
    non-regression test with a constant feature column that previously triggered
    the warning.
  • Changelog entry added.

Notes

A previous WIP PR (#21360) was closed; a maintainer suggested using
mean_variance_axis but the minimal clamp approach is less invasive and
preserves sparse matrix support without changing the computation path.

Eden Rochman 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
@github-actions github-actions Bot added module:feature_selection CI:Linter failure The linter CI is failing on this PR labels Mar 26, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Mar 27, 2026
@EdenRochmanSharabi

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

f_regression takes square root of negative values when constant columns are present

1 participant