Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TST check that we support multioutput custom scorer in RidgeCV #29884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 19, 2024
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
5 changes: 5 additions & 0 deletions doc/whats_new/v1.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ Changelog
for the calculation of test scores.
:pr:`29419` by :user:`Shruti Nath <snath-xoc>`.

- |Fix| :class:`linear_model.RidgeCV` now properly supports custom multioutput scorers
by letting the scorer manage the multioutput averaging. Previously, the predictions
and true targets were both squeezed to a 1D array before computing the error.
:pr:`29884` by :user:`Guillaume Lemaitre <glemaitre>`.

- |Fix| :class:`linear_model.RidgeCV` now properly uses predictions on the same scale as
the target seen during `fit`. These predictions are stored in `cv_results_` when
`scoring != None`. Previously, the predictions were rescaled by the square root of the
Expand Down
30 changes: 30 additions & 0 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,36 @@ def test_ridge_cv_multioutput_sample_weight(global_random_seed):
assert_allclose(ridge_cv.best_score_, -mean_squared_error(y, y_pred_loo))


def test_ridge_cv_custom_multioutput_scorer():
"""Check that `RidgeCV` works properly with a custom multioutput scorer."""
X, y = make_regression(n_targets=2, random_state=0)

def custom_error(y_true, y_pred):
errors = (y_true - y_pred) ** 2
mean_errors = np.mean(errors, axis=0)
if mean_errors.ndim == 1:
# case of multioutput
return -np.average(mean_errors, weights=[2, 1])
# single output - this part of the code should not be reached in the case of
# multioutput scoring
return -mean_errors # pragma: no cover

def custom_multioutput_scorer(estimator, X, y):
"""Multioutput score that give twice more importance to the second target."""
return -custom_error(y, estimator.predict(X))

ridge_cv = RidgeCV(scoring=custom_multioutput_scorer)
ridge_cv.fit(X, y)

cv = LeaveOneOut()
ridge = Ridge(alpha=ridge_cv.alpha_)
y_pred_loo = np.squeeze(
[ridge.fit(X[train], y[train]).predict(X[test]) for train, test in cv.split(X)]
)

assert_allclose(ridge_cv.best_score_, -custom_error(y, y_pred_loo))


# Metadata Routing Tests
# ======================

Expand Down