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

Skip to content

FIX RidgeCV works with multioutput and sample-weight non-default score #29877

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
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
7 changes: 1 addition & 6 deletions sklearn/linear_model/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2248,12 +2248,7 @@ def _score(self, *, predictions, y, n_y, scorer, score_params):
]
)
else:
_score = scorer(
identity_estimator,
predictions.ravel(),
y.ravel(),
**score_params,
)
_score = scorer(identity_estimator, predictions, y, **score_params)

return _score

Expand Down
26 changes: 26 additions & 0 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,32 @@ def test_ridge_cv_values_deprecated():
ridge.cv_values_


def test_ridge_cv_multioutput_sample_weight(global_random_seed):
"""Check that `RidgeCV` works properly with multioutput and sample_weight
when `scoring != None`.

We check the error reported by the RidgeCV is close to a naive LOO-CV using a
Ridge estimator.
"""
X, y = make_regression(n_targets=2, random_state=global_random_seed)
sample_weight = np.ones(shape=(X.shape[0],))

ridge_cv = RidgeCV(scoring="neg_mean_squared_error", store_cv_results=True)
ridge_cv.fit(X, y, sample_weight=sample_weight)

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


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

Expand Down
Loading