Skip to content
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: 7 additions & 0 deletions doc/whats_new/v1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ Changelog
:pr:`123456` by :user:`Joe Bloggs <joeongithub>`.
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 <jeremiedbb>`.

:mod:`sklearn.cluster`
......................

Expand Down
4 changes: 1 addition & 3 deletions sklearn/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
11 changes: 11 additions & 0 deletions sklearn/tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)