Skip to content

FIX prevent catastrophic cancellation in r_regression (#11395) - #34834

Open
FinalSunFlower wants to merge 2 commits into
scikit-learn:mainfrom
FinalSunFlower:fix/r-regression-cancellation-11395
Open

FIX prevent catastrophic cancellation in r_regression (#11395)#34834
FinalSunFlower wants to merge 2 commits into
scikit-learn:mainfrom
FinalSunFlower:fix/r-regression-cancellation-11395

Conversation

@FinalSunFlower

@FinalSunFlower FinalSunFlower commented Aug 28, 2026

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #11395.

What does this implement/fix? Explain your changes.

r_regression computes centered feature norms using a moment-based expression. Floating-point cancellation can make the mathematically non-negative centered sum of squares slightly negative for constant or near-constant columns, causing an invalid square root and propagating invalid correlations. This change clamps the intermediate squared norms to zero before sqrt, preserving existing results for active non-constant features and keeping dense, CSR, and CSC inputs consistent. Regression tests cover constant features, force_finite=False, sparse formats, high-magnitude cancellation, and active-feature preservation.

Introduce yourself

I am FinalSunFlower, contributing a numerically focused bug fix to scikit-learn's feature-selection utilities. I use scikit-learn for classical machine-learning workflows and was prompted by issue #11395 to improve the stability of r_regression on degenerate inputs.

AI usage disclosure

I used AI assistance for:

  • Research and understanding
  • Code generation (implementation and tests)
  • Test/benchmark generation
  • Documentation (including examples)

Any other comments?

Local CPU validation passed for the targeted regression tests, the complete feature-selection test module, Ruff checks, and formatting checks. The changelog entry is included in doc/whats_new/upcoming_changes/sklearn.feature_selection/34834.fix.rst.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

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