Skip to content

CalibratedClassifierCV loses groups with ensemble=False and metadata routing enabled #35523

Backlog

Description

@Y2XNishan

Introduce yourself

I’m Nishan, a CSE student learning more about scikit-learn’s calibration and cross-validation code. While investigating grouped calibration with OpenAI Codex, this failure was reproduced using synthetic data. I did not encounter it in a production application.

Codex assisted with source investigation and executed the reproducer and controls. It also helped prepare this report.

Describe the bug and give evidence about its user-facing impact

With metadata routing enabled, CalibratedClassifierCV using GroupKFold and ensemble=False raises ValueError even when groups are supplied to fit.

This prevents using the grouped splitter directly for non-ensemble calibration. Group-based splits are useful when related samples, such as observations from the same patient or device, must remain together. The example below uses synthetic groups, each containing both classes.

The same data succeeds with:

  • CalibratedClassifierCV using ensemble=True and routed groups.
  • CalibratedClassifierCV using ensemble=False and explicitly computed group-disjoint splits.
  • Direct cross_val_predict with params={"groups": groups}.

This appears related to #12052, which was closed after metadata-routing support was added. The non-ensemble path appears to remain affected: it passes only routed_params.estimator.fit to cross_val_predict, leaving out the splitter metadata containing groups.

Steps/Code to Reproduce

import numpy as np

from sklearn import config_context
from sklearn.calibration import CalibratedClassifierCV
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GroupKFold

X = np.array(
    [-2, 2, -1.5, 1.5, -1, 1, -0.5, 0.5]
).reshape(-1, 1)
y = np.tile([0, 1], 4)
groups = np.repeat(np.arange(4), 2)

with config_context(enable_metadata_routing=True):
    model = CalibratedClassifierCV(
        LogisticRegression(),
        cv=GroupKFold(n_splits=2),
        ensemble=False,
    )
    model.fit(X, y, groups=groups)

Expected Results

The supplied groups should reach GroupKFold, and fitting should complete successfully.

This should respect the same group-disjoint splits obtained from GroupKFold.split(X, y, groups). Passing those explicit splits to CalibratedClassifierCV with ensemble=False already succeeds.

Actual Results

Traceback (most recent call last):
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/repro.py", line 39, in <module>
    CalibratedClassifierCV(LogisticRegression(), cv=cv, ensemble=False).fit(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        X, y, groups=groups
        ^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/base.py", line 1403, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/calibration.py", line 569, in fit
    predictions = cross_val_predict(
        estimator=this_estimator,
    ...<5 lines>...
        params=routed_params.estimator.fit,
    )
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/utils/_param_validation.py", line 218, in wrapper
    return func(*args, **kwargs)
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/model_selection/_validation.py", line 1191, in cross_val_predict
    splits = list(cv.split(X, y, **routed_params.splitter.split))
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/model_selection/_split.py", line 411, in split
    for train, test in super().split(X, y, groups):
                       ~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/model_selection/_split.py", line 142, in split
    for test_index in self._iter_test_masks(X, y, groups):
                      ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/model_selection/_split.py", line 154, in _iter_test_masks
    for test_index in self._iter_test_indices(X, y, groups):
                      ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/sklearn/model_selection/_split.py", line 607, in _iter_test_indices
    raise ValueError("The 'groups' parameter should not be None.")
ValueError: The 'groups' parameter should not be None.

Versions

System:
    python: 3.13.18 | packaged by conda-forge | (main, Oct 26 2026, 11:38:18) [GCC 15.3.0]
executable: /var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/bin/python
   machine: Linux-6.18.58.1-microsoft-standard-WSL2-x86_64-with-glibc2.35

Python dependencies:
      sklearn: 1.10.dev0
          pip: None
   setuptools: None
        numpy: 2.5.5
        scipy: 1.18.1
       Cython: 3.2.5
       pandas: None
   matplotlib: None
       joblib: 1.6.0
threadpoolctl: 3.8.0
     narwhals: 2.29.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 12
         prefix: libscipy_openblas
       filepath: /var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/scipy.libs/libscipy_openblas-5f890258.so
        version: 0.3.31.dev
threading_layer: pthreads
   architecture: Haswell

       user_api: openmp
   internal_api: openmp
    num_threads: 12
         prefix: libgomp
       filepath: /var/tmp/oss-scipy-9209-cpu-a51fd39c/envs/scipy-12510678675253744734/envs/build/lib/libgomp.so.1.0.0
        version: None

       user_api: blas
   internal_api: openblas
    num_threads: 12
         prefix: libscipy_openblas
       filepath: /var/tmp/oss-sklearn-group-calibration-yCwDLHGU/venv/lib/python3.13/site-packages/numpy.libs/libscipy_openblas64_-4b78795a.so
        version: 0.3.34.237.2
threading_layer: pthreads
   architecture: Haswell

Tested upstream revision: 7c426b6d7f5eacc57abfd58880cf6187bd09cbdc

Interest in fixing the bug

Yes, I would be interested in working on a fix after maintainers confirm and triage the issue.

The source investigation suggests that the ensemble=False branch forwards estimator fit metadata to cross_val_predict but omits splitter metadata. I would investigate how to forward both correctly and add regression coverage for grouped cross-validation in both ensemble modes, while preserving sample-weight and estimator metadata routing.

I will wait for triage before opening a PR.

Activity

  1. added
    Needs Triage
    on Nov 25, 2026
  2. added
    Metadata Routing
    and removed
    Needs Triage
    on Nov 26, 2026
  3. betatim commented on Nov 26, 2026

    @betatim
    Member

    This needs the eyes of a maintainer who works on metadata routing like @StefanieSenger or @adrinjalali

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMetadata Routing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions