Skip to content

Commit 1577866

Browse files
authored
fix: Make _add_scorers compatible with sklearn 1.4 (#1062)
Addressing the issue specified in #916 (comment) Because in scikit-learn 1.4, `check_estimator` does not accept `estimator=None`. Luckily, we have the available estimator so we can provide it to `_add_scorers` as a workaround and since `scoring is not None` we don't have any change of behaviour anyway.
1 parent 9ce6e5b commit 1577866

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

skore/src/skore/sklearn/cross_validation/cross_validation_helpers.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _get_scorers_to_add(estimator, y) -> dict[str, Any]:
5858
return {}
5959

6060

61-
def _add_scorers(scorers, scorers_to_add):
61+
def _add_scorers(scorers, scorers_to_add, estimator):
6262
"""Expand `scorers` with more scorers.
6363
6464
The type of the resulting scorers object is dependent on the type of the input
@@ -79,6 +79,8 @@ def _add_scorers(scorers, scorers_to_add):
7979
The scorer(s) to expand.
8080
scorers_to_add : dict[str, str]
8181
The scorers to be added.
82+
estimator : estimator
83+
A scikit-learn estimator.
8284
8385
Returns
8486
-------
@@ -89,10 +91,12 @@ def _add_scorers(scorers, scorers_to_add):
8991
in `scorers`).
9092
"""
9193
if scorers is None or isinstance(scorers, str):
92-
new_scorers, added_scorers = _add_scorers({"score": scorers}, scorers_to_add)
94+
new_scorers, added_scorers = _add_scorers(
95+
{"score": scorers}, scorers_to_add, estimator
96+
)
9397
elif isinstance(scorers, (list, tuple)):
9498
new_scorers, added_scorers = _add_scorers(
95-
{s: s for s in scorers}, scorers_to_add
99+
{s: s for s in scorers}, scorers_to_add, estimator
96100
)
97101
elif isinstance(scorers, dict):
98102
# User-defined metrics have priority
@@ -103,8 +107,11 @@ def _add_scorers(scorers, scorers_to_add):
103107
from sklearn.metrics._scorer import _MultimetricScorer
104108

105109
internal_scorer = _MultimetricScorer(
110+
# NOTE: we pass `estimator` to `check_scoring` for compatibility with
111+
# scikit-learn 1.4. However, because `scoring` is never `None`, this
112+
# estimator will not have any effect.
106113
scorers={
107-
name: check_scoring(estimator=None, scoring=scoring)
114+
name: check_scoring(estimator=estimator, scoring=scoring)
108115
if isinstance(scoring, str)
109116
else scoring
110117
for name, scoring in scorers_to_add.items()

skore/src/skore/sklearn/cross_validation/cross_validation_reporter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def __init__(self, *args, **kwargs):
134134

135135
# Extend scorers with other relevant scorers
136136
scorers_to_add = _get_scorers_to_add(self.estimator, self.y)
137-
self._scorers, added_scorers = _add_scorers(self.scorers, scorers_to_add)
137+
self._scorers, added_scorers = _add_scorers(
138+
self.scorers, scorers_to_add, self.estimator
139+
)
138140

139141
self._cv_results = sklearn.model_selection.cross_validate(
140142
*args,

0 commit comments

Comments
 (0)