Skip to content

Commit

Permalink
FIX Allow alpha 0 when not using LOO in RidgeCV (scikit-learn#28425)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucyleeow authored Feb 17, 2024
1 parent 87f65d1 commit aaf215c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
24 changes: 13 additions & 11 deletions doc/modules/linear_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,14 @@ This method has the same order of complexity as
Setting the regularization parameter: leave-one-out Cross-Validation
--------------------------------------------------------------------

:class:`RidgeCV` implements ridge regression with built-in
cross-validation of the alpha parameter. The object works in the same way
as GridSearchCV except that it defaults to Leave-One-Out Cross-Validation::
:class:`RidgeCV` and :class:`RidgeClassifierCV` implement ridge
regression/classification with built-in cross-validation of the alpha parameter.
They work in the same way as :class:`~sklearn.model_selection.GridSearchCV` except
that it defaults to efficient Leave-One-Out :term:`cross-validation`.
When using the default :term:`cross-validation`, alpha cannot be 0 due to the
formulation used to calculate Leave-One-Out error. See [RL2007]_ for details.

Usage example::

>>> import numpy as np
>>> from sklearn import linear_model
Expand All @@ -211,16 +216,13 @@ cross-validation with :class:`~sklearn.model_selection.GridSearchCV`, for
example `cv=10` for 10-fold cross-validation, rather than Leave-One-Out
Cross-Validation.

|details-start|
**References**
|details-split|
.. topic:: References:

* "Notes on Regularized Least Squares", Rifkin & Lippert (`technical report
<http://cbcl.mit.edu/publications/ps/MIT-CSAIL-TR-2007-025.pdf>`_,
`course slides
<https://www.mit.edu/~9.520/spring07/Classes/rlsslides.pdf>`_).

|details-end|
.. [RL2007] "Notes on Regularized Least Squares", Rifkin & Lippert (`technical report
<http://cbcl.mit.edu/publications/ps/MIT-CSAIL-TR-2007-025.pdf>`_,
`course slides
<https://www.mit.edu/~9.520/spring07/Classes/rlsslides.pdf>`_).
.. _lasso:

Expand Down
24 changes: 17 additions & 7 deletions sklearn/linear_model/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,12 +2188,21 @@ def fit(self, X, y, sample_weight=None):
"""
cv = self.cv

check_scalar_alpha = partial(
check_scalar,
target_type=numbers.Real,
min_val=0.0,
include_boundaries="neither",
)
# `_RidgeGCV` does not work for alpha = 0
if cv is None:
check_scalar_alpha = partial(
check_scalar,
target_type=numbers.Real,
min_val=0.0,
include_boundaries="neither",
)
else:
check_scalar_alpha = partial(
check_scalar,
target_type=numbers.Real,
min_val=0.0,
include_boundaries="left",
)

if isinstance(self.alphas, (np.ndarray, list, tuple)):
n_alphas = 1 if np.ndim(self.alphas) == 0 else len(self.alphas)
Expand Down Expand Up @@ -2272,7 +2281,7 @@ class RidgeCV(
Alpha corresponds to ``1 / (2C)`` in other linear models such as
:class:`~sklearn.linear_model.LogisticRegression` or
:class:`~sklearn.svm.LinearSVC`.
If using Leave-One-Out cross-validation, alphas must be positive.
If using Leave-One-Out cross-validation, alphas must be strictly positive.
fit_intercept : bool, default=True
Whether to calculate the intercept for this model. If set
Expand Down Expand Up @@ -2439,6 +2448,7 @@ class RidgeClassifierCV(_RoutingNotSupportedMixin, _RidgeClassifierMixin, _BaseR
Alpha corresponds to ``1 / (2C)`` in other linear models such as
:class:`~sklearn.linear_model.LogisticRegression` or
:class:`~sklearn.svm.LinearSVC`.
If using Leave-One-Out cross-validation, alphas must be strictly positive.
fit_intercept : bool, default=True
Whether to calculate the intercept for this model. If set
Expand Down

0 comments on commit aaf215c

Please sign in to comment.