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

Skip to content

Commit 25bc29a

Browse files
authored
FIX RidgeCV works with multioutput and sample-weight non-default score (#29877)
1 parent 199322d commit 25bc29a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

sklearn/linear_model/_ridge.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -2270,12 +2270,7 @@ def _score(self, *, predictions, y, n_y, scorer, score_params):
22702270
]
22712271
)
22722272
else:
2273-
_score = scorer(
2274-
identity_estimator,
2275-
predictions.ravel(),
2276-
y.ravel(),
2277-
**score_params,
2278-
)
2273+
_score = scorer(identity_estimator, predictions, y, **score_params)
22792274

22802275
return _score
22812276

sklearn/linear_model/tests/test_ridge.py

+26
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,32 @@ def test_ridge_cv_values_deprecated():
22522252
ridge.cv_values_
22532253

22542254

2255+
def test_ridge_cv_multioutput_sample_weight(global_random_seed):
2256+
"""Check that `RidgeCV` works properly with multioutput and sample_weight
2257+
when `scoring != None`.
2258+
2259+
We check the error reported by the RidgeCV is close to a naive LOO-CV using a
2260+
Ridge estimator.
2261+
"""
2262+
X, y = make_regression(n_targets=2, random_state=global_random_seed)
2263+
sample_weight = np.ones(shape=(X.shape[0],))
2264+
2265+
ridge_cv = RidgeCV(scoring="neg_mean_squared_error", store_cv_results=True)
2266+
ridge_cv.fit(X, y, sample_weight=sample_weight)
2267+
2268+
cv = LeaveOneOut()
2269+
ridge = Ridge(alpha=ridge_cv.alpha_)
2270+
y_pred_loo = np.squeeze(
2271+
[
2272+
ridge.fit(X[train], y[train], sample_weight=sample_weight[train]).predict(
2273+
X[test]
2274+
)
2275+
for train, test in cv.split(X)
2276+
]
2277+
)
2278+
assert_allclose(ridge_cv.best_score_, -mean_squared_error(y, y_pred_loo))
2279+
2280+
22552281
# Metadata Routing Tests
22562282
# ======================
22572283

0 commit comments

Comments
 (0)