Skip to content

LogisticRegressionCV with scoring = "neg_brier_score" fails when a CV test split is missing a class. #33207

Closed

Description

@un1u3

neg_brier_score uses brier_score_loss, which requires labels when y_true does not include all classes. In LogisticRegressionCV, the scorer is called without labels, even though the estimator is trained on all classes and probabilities are emitted for all classes. This causes a ValueError if any test fold misses a class. There is already a FIXMEin the source noting exactly this. The scoring should pass labels=classes (or equivalent) to be robust.

Image

Minimal Reproduction

import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegressionCV
import sklearn

print("sklearn", sklearn.__version__)

X, y = make_classification(
    n_samples=90,
    n_features=8,
    n_informative=6,
    n_redundant=0,
    n_classes=3,
    n_clusters_per_class=1,
    random_state=0,
)

idx0 = np.flatnonzero(y == 0)
idx1 = np.flatnonzero(y == 1)
idx2 = np.flatnonzero(y == 2)

# Fold 0 test set has only classes 0 and 1
test0 = np.concatenate([idx0[:10], idx1[:10]])
train0 = np.setdiff1d(np.arange(len(y)), test0)

# Fold 1 test set has only class 2
test1 = idx2[:10]
train1 = np.setdiff1d(np.arange(len(y)), test1)

cv = [(train0, test0), (train1, test1)]

clf = LogisticRegressionCV(
    cv=cv,
    scoring="neg_brier_score",
    solver="lbfgs",
    max_iter=200,
)

clf.fit(X, y)

Observed Error

ValueError: y_true and y_prob contain different number of classes: 2 vs 3. 
Please provide the true labels explicitly through the labels argument. Classes found in y_true: [0 1]

Suggest Fix (also mentioned in comments by a contributer)

When scoring is neg_brier_score, pass labels=classes via score_params or directly to the scorer call so the full class set is used

Activity

  1. added
    Needs Triage
    on Feb 4, 2026
  2. rohandhar6824-debug commented on Feb 5, 2026

    @rohandhar6824-debug

    I've reproduced the issue and identified the root cause. The problem occurs in _log_reg_scoring_path() when using neg_brier_score or similar metrics that rely on brier_score_loss. When a CV test split is missing a class, the scorer needs explicit labels parameter to know about all classes that the model was trained on.

    Proposed fix: In _log_reg_scoring_path(), when the scoring method is "neg_brier_score" or "brier_score_loss", we should pass labels=classes to the scorer call. This ensures the Brier score calculation considers all classes, even if they're not present in the test fold.

    The fix is minimal and addresses the exact scenario described in the FIXME comment and issue #33207 that's already in the code. This will make LogisticRegressionCV robust to class imbalance in cross-validation folds when using Brier score-based metrics.

  3. un1u3 commented on Feb 5, 2026

    @un1u3
    ContributorAuthor

    I've reproduced the issue and identified the root cause. The problem occurs in _log_reg_scoring_path() when using neg_brier_score or similar metrics that rely on brier_score_loss. When a CV test split is missing a class, the scorer needs explicit labels parameter to know about all classes that the model was trained on.

    Proposed fix: In _log_reg_scoring_path(), when the scoring method is "neg_brier_score" or "brier_score_loss", we should pass labels=classes to the scorer call. This ensures the Brier score calculation considers all classes, even if they're not present in the test fold.

    The fix is minimal and addresses the exact scenario described in the FIXME comment and issue #33207 that's already in the code. This will make LogisticRegressionCV robust to class imbalance in cross-validation folds when using Brier score-based metrics.

    the implementation checks whether the scorer supports a labels parmeter before passing it. This fixes neg_brier_score as noted in FIXME, and also covers closely related probabilistic scorers (like neg_log_loss) that rely on full class information when some classes are absent from a CV fold.

    For built-in sklearn scorers, labels is added to a shallow copy of the scorer’s parameters to avoid mutating user-provided objects and to respect existing metadata routing behavior. No user-facing configuration is required

  4. rohandhar6824-debug commented on Feb 5, 2026

    @rohandhar6824-debug

    Thank you.
    Implemented your exact approach

  5. removed
    Needs Triage
    on Feb 5, 2026
  6. adrinjalali commented on Feb 5, 2026

    @adrinjalali
    Member

    Duplicate of #15389

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions