Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

CLN Support _MultimetricScorer directly for internal methods #28359

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 2 commits into from
Feb 6, 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
28 changes: 10 additions & 18 deletions 28 sklearn/model_selection/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,18 +781,11 @@ def _select_best_index(refit, refit_metric, results):
best_index = results[f"rank_test_{refit_metric}"].argmin()
return best_index

def _get_scorers(self, convert_multimetric):
def _get_scorers(self):
"""Get the scorer(s) to be used.

This is used in ``fit`` and ``get_metadata_routing``.

Parameters
----------
convert_multimetric : bool
Whether to convert a dict of scorers to a _MultimetricScorer. This
is used in ``get_metadata_routing`` to include the routing info for
multiple scorers.

Returns
-------
scorers, refit_metric
Expand All @@ -807,10 +800,9 @@ def _get_scorers(self, convert_multimetric):
scorers = _check_multimetric_scoring(self.estimator, self.scoring)
self._check_refit_for_multimetric(scorers)
refit_metric = self.refit
if convert_multimetric and isinstance(scorers, dict):
scorers = _MultimetricScorer(
scorers=scorers, raise_exc=(self.error_score == "raise")
)
scorers = _MultimetricScorer(
scorers=scorers, raise_exc=(self.error_score == "raise")
)

return scorers, refit_metric

Expand Down Expand Up @@ -866,10 +858,7 @@ def fit(self, X, y=None, **params):
Instance of fitted estimator.
"""
estimator = self.estimator
# Here we keep a dict of scorers as is, and only convert to a
# _MultimetricScorer at a later stage. Issue:
# https://github.com/scikit-learn/scikit-learn/issues/27001
scorers, refit_metric = self._get_scorers(convert_multimetric=False)
scorers, refit_metric = self._get_scorers()

X, y = indexable(X, y)
params = _check_method_params(X, params=params)
Expand Down Expand Up @@ -1015,7 +1004,10 @@ def evaluate_candidates(candidate_params, cv=None, more_results=None):
self.feature_names_in_ = self.best_estimator_.feature_names_in_

# Store the only scorer not as a dict for single metric evaluation
self.scorer_ = scorers
if isinstance(scorers, _MultimetricScorer):
self.scorer_ = scorers._scorers
else:
self.scorer_ = scorers

self.cv_results_ = results
self.n_splits_ = n_splits
Expand Down Expand Up @@ -1147,7 +1139,7 @@ def get_metadata_routing(self):
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)

scorer, _ = self._get_scorers(convert_multimetric=True)
scorer, _ = self._get_scorers()
router.add(
scorer=scorer,
method_mapping=MethodMapping()
Expand Down
25 changes: 7 additions & 18 deletions 25 sklearn/model_selection/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,11 @@ def cross_validate(
scorers = check_scoring(estimator, scoring)
else:
scorers = _check_multimetric_scoring(estimator, scoring)
scorers = _MultimetricScorer(
scorers=scorers, raise_exc=(error_score == "raise")
)

if _routing_enabled():
# `cross_validate` will create a `_MultiMetricScorer` if `scoring` is a
# dict at a later stage. We need the same object for the purpose of
# routing. However, creating it here and passing it around would create
# a much larger diff since the dict is used in many places.
if isinstance(scorers, dict):
_scorer = _MultimetricScorer(
scorers=scorers, raise_exc=(error_score == "raise")
)
else:
_scorer = scorers
# For estimators, a MetadataRouter is created in get_metadata_routing
# methods. For these router methods, we create the router to use
# `process_routing` on it.
Expand All @@ -386,7 +379,7 @@ def cross_validate(
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)
.add(
scorer=_scorer,
scorer=scorers,
method_mapping=MethodMapping().add(caller="fit", callee="score"),
)
)
Expand Down Expand Up @@ -901,8 +894,8 @@ def _fit_and_score(
if error_score == "raise":
raise
elif isinstance(error_score, numbers.Number):
if isinstance(scorer, dict):
test_scores = {name: error_score for name in scorer}
if isinstance(scorer, _MultimetricScorer):
test_scores = {name: error_score for name in scorer._scorers}
if return_train_score:
train_scores = test_scores.copy()
else:
Expand Down Expand Up @@ -966,13 +959,9 @@ def _fit_and_score(
def _score(estimator, X_test, y_test, scorer, score_params, error_score="raise"):
"""Compute the score(s) of an estimator on a given test set.

Will return a dict of floats if `scorer` is a dict, otherwise a single
Will return a dict of floats if `scorer` is a _MultiMetricScorer, otherwise a single
float is returned.
"""
if isinstance(scorer, dict):
# will cache method calls if needed. scorer() returns a dict
scorer = _MultimetricScorer(scorers=scorer, raise_exc=(error_score == "raise"))

score_params = {} if score_params is None else score_params

try:
Expand Down
9 changes: 7 additions & 2 deletions 9 sklearn/model_selection/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
precision_score,
r2_score,
)
from sklearn.metrics._scorer import _MultimetricScorer
from sklearn.model_selection import (
GridSearchCV,
GroupKFold,
Expand Down Expand Up @@ -2323,7 +2324,9 @@ def three_params_scorer(i, j, k):
),
(
True,
{"sc1": three_params_scorer, "sc2": three_params_scorer},
_MultimetricScorer(
scorers={"sc1": three_params_scorer, "sc2": three_params_scorer}
),
3,
(1, 3),
(0, 1),
Expand All @@ -2332,7 +2335,9 @@ def three_params_scorer(i, j, k):
),
(
False,
{"sc1": three_params_scorer, "sc2": three_params_scorer},
_MultimetricScorer(
scorers={"sc1": three_params_scorer, "sc2": three_params_scorer}
),
10,
(1, 3),
(0, 1),
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.