From 6c2c06eb8d0a284270566e20215e95e19b1859e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= Date: Mon, 15 Apr 2024 19:53:00 +0200 Subject: [PATCH 1/3] numpy's behavior of element-wise comparison with strings may be unexpected --- sklearn/calibration.py | 4 +--- sklearn/tests/test_calibration.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/sklearn/calibration.py b/sklearn/calibration.py index b02236eb600fe..40d3e5363a7e0 100644 --- a/sklearn/calibration.py +++ b/sklearn/calibration.py @@ -388,9 +388,7 @@ def fit(self, X, y, sample_weight=None, **fit_params): n_folds = self.cv.n_splits else: n_folds = None - if n_folds and np.any( - [np.sum(y == class_) < n_folds for class_ in self.classes_] - ): + if n_folds and np.any(np.unique(y, return_counts=True)[1] < n_folds): raise ValueError( f"Requesting {n_folds}-fold " "cross-validation but provided less than " diff --git a/sklearn/tests/test_calibration.py b/sklearn/tests/test_calibration.py index eb7204b366729..833ef2ea7e558 100644 --- a/sklearn/tests/test_calibration.py +++ b/sklearn/tests/test_calibration.py @@ -1088,3 +1088,14 @@ def predict_proba(self, X): calibrator = CalibratedClassifierCV(model) # Does not raise an error calibrator.fit(*data) + + +def test_error_less_class_samples_than_folds(): + """Check that CalibratedClassifierCV works with string targets. + + non-regression test for issue #28841. + """ + X = np.random.normal(size=(20, 3)) + y = ["a"] * 10 + ["b"] * 10 + + CalibratedClassifierCV(cv=3).fit(X, y) From d740430dcdd67182068a598f6e6baba6a4da0996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= Date: Mon, 15 Apr 2024 20:53:04 +0200 Subject: [PATCH 2/3] changelog entry --- doc/whats_new/v1.5.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/whats_new/v1.5.rst b/doc/whats_new/v1.5.rst index 1fc651e303e56..d075f4049d3ac 100644 --- a/doc/whats_new/v1.5.rst +++ b/doc/whats_new/v1.5.rst @@ -127,6 +127,13 @@ Changelog :pr:`123456` by :user:`Joe Bloggs `. where 123455 is the *pull request* number, not the issue number. +:mod:`sklearn.calibration` +......................... + +- |Fix| Fixed a regression in :class:`calibration.CalibratedClassifierCV` where + an error was wrongly raised with string targets. + :pr:`28843` by :user:`Jérémie du Boisberranger `. + :mod:`sklearn.cluster` ...................... From 9130934fa90c36841cb2c24779932ef9b0e38524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= Date: Mon, 15 Apr 2024 22:36:49 +0200 Subject: [PATCH 3/3] Update doc/whats_new/v1.5.rst --- doc/whats_new/v1.5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats_new/v1.5.rst b/doc/whats_new/v1.5.rst index d075f4049d3ac..adbd64f2f4477 100644 --- a/doc/whats_new/v1.5.rst +++ b/doc/whats_new/v1.5.rst @@ -128,7 +128,7 @@ Changelog where 123455 is the *pull request* number, not the issue number. :mod:`sklearn.calibration` -......................... +.......................... - |Fix| Fixed a regression in :class:`calibration.CalibratedClassifierCV` where an error was wrongly raised with string targets.