Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats_new/v0.23.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Changelog
`store_cv_values` is `True`.
:pr:`15652` by :user:`Jérôme Dockès <jeromedockes>`.

- |Fix| add `best_score_` attribute to :class:`linear_model.RidgeCV` and
:class:`linear_model.RidgeClassifierCV`.
:pr:`15653` by :user:`Jérôme Dockès <jeromedockes>`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add yourself?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is fine :)

Copy link
Member Author

Choose a reason for hiding this comment

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

it was originally @jeromedockes, I am just splitting the PR such that it is easy to review


:mod:`sklearn.preprocessing`
............................

Expand Down
10 changes: 9 additions & 1 deletion sklearn/linear_model/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ def fit(self, X, y, sample_weight=None):
store_cv_values=self.store_cv_values)
estimator.fit(X, y, sample_weight=sample_weight)
self.alpha_ = estimator.alpha_
self.best_score_ = estimator.best_score_
if self.store_cv_values:
self.cv_values_ = estimator.cv_values_
else:
Expand All @@ -1591,6 +1592,7 @@ def fit(self, X, y, sample_weight=None):
gs.fit(X, y, sample_weight=sample_weight)
estimator = gs.best_estimator_
self.alpha_ = gs.best_estimator_.alpha
self.best_score_ = gs.best_score_

self.coef_ = estimator.coef_
self.intercept_ = estimator.intercept_
Expand Down Expand Up @@ -1693,6 +1695,9 @@ class RidgeCV(MultiOutputMixin, RegressorMixin, _BaseRidgeCV):
alpha_ : float
Estimated regularization parameter.

best_score_ : float
Mean cross-validated score of the estimator with the best alpha found.

Examples
--------
>>> from sklearn.datasets import load_diabetes
Expand Down Expand Up @@ -1795,7 +1800,10 @@ class RidgeClassifierCV(LinearClassifierMixin, _BaseRidgeCV):
``fit_intercept = False``.

alpha_ : float
Estimated regularization parameter
Estimated regularization parameter.

best_score_ : float
Mean cross-validated score of the estimator with the best alpha found.

classes_ : array of shape (n_classes,)
The classes labels.
Expand Down
20 changes: 17 additions & 3 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,17 +664,31 @@ def _test_ridge_cv(filter_):

@pytest.mark.parametrize(
"ridge, make_dataset",
[(RidgeCV(), make_regression),
(RidgeClassifierCV(), make_classification)]
[(RidgeCV(store_cv_values=False), make_regression),
(RidgeClassifierCV(store_cv_values=False), make_classification)]
)
def test_ridge_gcv_cv_values_not_stored(ridge, make_dataset):
# Check that `cv_values_` is not stored when store_cv_values is False
X, y = make_dataset(n_samples=6, random_state=42)
ridge.set_params(store_cv_values=False)
ridge.fit(X, y)
assert not hasattr(ridge, "cv_values_")


@pytest.mark.parametrize(
"ridge, make_dataset",
[(RidgeCV(), make_regression),
(RidgeClassifierCV(), make_classification)]
)
@pytest.mark.parametrize("cv", [None, 3])
def test_ridge_best_score(ridge, make_dataset, cv):
# check that the best_score_ is store
X, y = make_dataset(n_samples=6, random_state=42)
ridge.set_params(store_cv_values=False, cv=cv)
ridge.fit(X, y)
assert hasattr(ridge, "best_score_")
assert isinstance(ridge.best_score_, float)


def _test_ridge_diabetes(filter_):
ridge = Ridge(fit_intercept=False)
ridge.fit(filter_(X_diabetes), y_diabetes)
Expand Down