Repository navigation
FIX Clamp variance to zero in f_regression for constant columns - #34197
Closed
EdenRochmanSharabi wants to merge 2 commits into
Closed
EdenRochmanSharabi wants to merge 2 commits into
EdenRochmanSharabi wants to merge 2 commits into
Conversation
When a feature column is constant, catastrophic cancellation in row_norms(X.T, squared=True) - n_samples * X_means**2 can produce small negative values, causing np.sqrt to emit a RuntimeWarning and return NaN. Clamp the result to zero before taking the square root. Closes scikit-learn#11395
Contributor
Author
|
Closing to make room for other PRs. Happy to reopen if needed. |
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.
Reference Issues/PRs
Closes #11395.
What does this implement/fix? Explain your changes.
In
r_regression, whichf_regressioncalls, the per-column scale is computed assqrt(row_norms(X.T, squared=True) - n_samples * X_means**2). For a constant columnthe variance is mathematically zero, but catastrophic cancellation makes the
subtraction land on a small negative value, so
np.sqrtemits aRuntimeWarningandreturns
NaN. TheNaNpropagates into the correlation coefficient, whereforce_finite=Truecollapses it back to 0.0, so the visible symptom is the spuriouswarning.
This clamps the intermediate result to zero with
np.maximumbefore the square root.Added
test_f_regression_constant_column, which fails onmainwith aRuntimeWarningand passes here.AI usage disclosure
I used AI assistance for:
The fix itself was written by hand. AI was used to double check the diagnosis and to
review the change, not to generate it.
Any other comments?
The diagnosis in #11395 is @Lawson-Darrow's and they had offered to open a PR with a
regression test, so apologies for getting ahead of that. They also asked whether
mean_variance_axiswould be preferable to a clamp, following @agramfort's earliersuggestion. I went with the clamp because it keeps the dense and sparse paths
identical and adds no second pass over the data, but I am happy to switch, or to close
this in favour of their PR.