Skip to content

Fix LogisticRegressionCV scoring when CV folds miss class labels - #33209

Closed
un1u3 wants to merge 3 commits into
scikit-learn:mainfrom
un1u3:fix-logregcv-brier-score
Closed

un1u3 wants to merge 3 commits into
scikit-learn:mainfrom
un1u3:fix-logregcv-brier-score

Conversation

@un1u3

@un1u3 un1u3 commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

fixes #33207
LogisticRegressionCV can fail with probabilistic scorers such as neg_brier_score and neg_log_loss when a cross-validation test fold does not contain all class labels.

The failure occurs because _log_reg_scoring_path calls the scorer without passing the full set of class labels. In this case, y_true has fewer classes than the predicted probability matrix, causing a shape mismatch inside the scorer.

Changes

  • Update _log_reg_scoring_path to pass labels=classes to scorers that support it.
  • For built-in sklearn scorers, inject labels into a shallow copy of the scorer’s internal keyword arguments to avoid mutating user objects.
  • For custom callable scorers, pass labels only if explicitly supported by the callable signature.
  • Add a regression test covering missing-class folds for probabilistic scorers.

Result

Prevents crashes when CV folds lack class labels.
Preserves existing scorer behavior and state.
Resolves the FIXME noted in _log_reg_scoring_path.

Tests

  • sklearn/linear_model: 2,346 passed
  • sklearn/metrics: 3,083 passed
  • New Regression Test: Verified & passed

@github-actions github-actions Bot added module:linear_model CI:Linter failure The linter CI is failing on this PR labels Feb 5, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Feb 5, 2026
@betatim

betatim commented Feb 5, 2026

Copy link
Copy Markdown
Member

This looks like a legitimate problem that needs solving. Pinging @lorentzenchr because git blame suggests you left the FIXME comment. Also @adrinjalali because it seems like there is some interplay with metadata routing.

I'm not sure if the proposed fix is the right thing to do, it seems quite complicated and like it reaches quite deep into the "inners" of scorers. While exploring alternatives I thought that manually (via pdb.set_trace()) modifying score_params to include the labels would be a way to fix this. However when I do that I get an error related to metadata routing:

ValueError: Passing extra keyword arguments to make_scorer(brier_score_loss, greater_is_better=False, response_method='predict_proba') is only supported if enable_metadata_routing=True, which you can set using `sklearn.set_config`. See the User Guide <https://scikit-learn.org/stable/metadata_routing.html> for more details. Extra parameters passed are: {'labels'}

I'm not sure what to do about that. To me it seems like adding a "labels" entry to score_params would be the right thing to do. It would use existing infrastructure for passing around additional keyword arguments.

However, maybe Christian has some thoughts on how to solve this that he remembers from when he wrote the FIXME comment.

@un1u3 thanks for reporting this and proposing a fix. For the moment I'd wait with making more changes until we hear from the above two maintainers.

@adrinjalali

Copy link
Copy Markdown
Member

Duplicate of #32828 and the CI fails.

However, this might give an idea for the other PR.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants