Skip to content

FIX instability of _RidgeGCV - #33020

Merged
lorentzenchr merged 73 commits into
scikit-learn:mainfrom
antoinebaker:refactor_ridge_gcv
Feb 23, 2026
Merged

lorentzenchr merged 73 commits into
scikit-learn:mainfrom
antoinebaker:refactor_ridge_gcv

Conversation

@antoinebaker

@antoinebaker antoinebaker commented Jan 7, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #32459 and supersedes #32506

What does this implement/fix? Explain your changes.

This fixes numerical errors in the small alpha regime by refactoring the solve/decompose methods:

  • solve now returns the leave-one-out errors looe and the primal coeffcient coef instead of the c and G_inverse_diag
  • the helper _compute_gram and _compute_cov reuse the precomputed X_mean
  • estimation of looe and coef is adapted to each method for best numerical stability
  • new option cov to force use eigendecomposition of the covariance matrix
  • new option gram to force use eigendecomposition of the gram matrix

The gcv_mode options have been modified to always pick the cheaper option

  • option eigen now switches to the cheaper option: gram if n < p and cov if p < n
  • option svd, not implemented for sparse X, fallbacks to eigen when X is sparse.
  • in main, eigen always picks gramand svd always fallbacks to cov for sparse X (whatever the shape of X), which can explain the poor benchmark observed in FIX instability of gcv_mode="svd" in RidgeCV #32506.

TODO

  • make it array api compliant
  • benchmark memory/time compared to main
  • update _RidgeGCV docstring, especially math explanations
  • ensure proper input validation on integer feature values and add non-regression test,
  • parametrize some LOO equivalence tests to use float32 inputs: if results are bad, consider re-add the float32 to float64 upcast...
  • understand and document which gcv_mode and problem shapes are capable of recovering the minimum norm solution in the small alpha limit.

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked the code and the math yet, but the fact that the tests pass and the examples still look good is very promising.

I also ran some quick interactive timeit experiments and the performance seems good (including with torch/MPS) compared to main.

However, I found a performance regression for the linear regression panel of the examples/ensemble/plot_stack_predictors.py example:

image

in main, it runs in less than 0.01s: https://scikit-learn.org/dev/auto_examples/ensemble/plot_stack_predictors.html

Maybe this is because of a bad "auto" policy for that case?

Please also find a first pass of feedback below.

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved

@ogrisel ogrisel left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please extend the test test_ridge_cv_results_predictions to check that it works both for n_samples > n_features and n_features < n_samples with the auto/eigen solver?

EDIT: and similarly for test_ridge_cv_multioutput_sample_weight and test_ridge_cv_custom_multioutput_scorer.

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM now. Thank you for the updates @antoinebaker

I just need to review the tests

@lorentzenchr lorentzenchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another bit.

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
# All other cases ("auto", "eigen", "svd" with sparse X)
# fallbacks to gram (n <= p) or cov (p < n)
n, p = X.shape
return "gram" if n <= p else "cov"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the intercept play a role for p <=n?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, do you mean a) to choose between gram vs cov for the gcv_mode or b) in the computations afterwards ?

a) gram passes test_regularization_limits_ridge_gcv for the edge case n=p with/without intercept but cov doesn't.
b) In all cases (svd, cov, gram), the intercept is handled by centering X, y

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant a)

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Outdated

loov = (KG^-1Y - diag(KG^-1)Y) / diag(I-KG^-1)
The solution of the regularized least square (fitted `coef_`) is given by:
w = H^-1 X^T y = X^T c where c = G^-1 y.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe small c os not the best name here.

@antoinebaker antoinebaker Feb 20, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I used the variable name of the previous code. Maybe something like w -> coef, and c -> dual_coef, and d -> diag_Ginv could work ? But on the other hand it's nice to keep a one letter variable name (the equations and code are left aligned and easier to read)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe don't overthink it. It is not that important. I like single latter variables.

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Outdated
References
----------
[1] http://cbcl.mit.edu/publications/ps/MIT-CSAIL-TR-2007-025.pdf
[1] https://dspace.mit.edu/bitstream/handle/1721.1/37318/MIT-CSAIL-TR-2007-025.pdf

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you properly cite this reference, name and title and so on?

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
@OmarManzoor OmarManzoor removed the Waiting for Second Reviewer First reviewer is done, need a second one! label Feb 20, 2026

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more comments

antoinebaker and others added 3 commits February 20, 2026 15:01
Co-authored-by: Christian Lorentzen <[email protected]>
Co-authored-by: Christian Lorentzen <[email protected]>
Co-authored-by: Omar Salman <[email protected]>
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good from my side. Thank you for the PR @antoinebaker

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Feb 23, 2026
@lorentzenchr

Copy link
Copy Markdown
Member

@antoinebaker This was quite an endeavor, from a "small" numerical" fix to almost a rewrite of RidgeCV. Thanks for your endurance.
Someone will merge once CI/CD is green.

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Feb 23, 2026
@lorentzenchr
lorentzenchr enabled auto-merge (squash) February 23, 2026 10:01
@antoinebaker

Copy link
Copy Markdown
Member Author

Thanks for the careful review @OmarManzoor and @lorentzenchr !

@lorentzenchr
lorentzenchr merged commit ce4f234 into scikit-learn:main Feb 23, 2026
30 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Feb 23, 2026
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.

BUG RidgeCV with gcv_mode="svd" is unstable when alpha is very small

4 participants