Skip to content

FIX LogisticRegressionCV with CV split with missing class labels - #32747

Merged
lesteve merged 9 commits into
scikit-learn:mainfrom
lorentzenchr:fix_logregcv
Nov 21, 2025
Merged

lesteve merged 9 commits into
scikit-learn:mainfrom
lorentzenchr:fix_logregcv

Conversation

@lorentzenchr

@lorentzenchr lorentzenchr commented Nov 19, 2025 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fix #32748 and #30832

Leftover from #32073 (comment).

What does this implement/fix? Explain your changes.

If a CV fold in LogisticRegressionCV has not all classes present, the result is more or less random (certain solvers emit convergence warnings). This PR adds a test (CI will first fail) and then fix the test.

Any other comments?

@github-actions

github-actions Bot commented Nov 19, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 1df363e. Link to the linter CI: here

@lorentzenchr

Copy link
Copy Markdown
Member Author

CD fc3e82e fails as expected in test_multinomial_cv_iris.

@lorentzenchr

Copy link
Copy Markdown
Member Author

@lesteve fyi, up to you to include it either in 1.8.0 or 1.8.1.

else:
score_params = score_params or {}
score_params = _check_method_params(X=X, params=score_params, indices=test)
# FIXME: If scoring = "neg_brier_score" and if not all class labels

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a custom scorers, I think the caller is responsible for creating a scorer with labels set, but it's not obvious.

Not for this PR: I have not looked closely, but we can inspect the scoring object to see if it contains a metric that supports labels and pass it in. Although this idea feels like a hack.

@lesteve

lesteve commented Nov 20, 2025

Copy link
Copy Markdown
Member

Thanks for the PR @lorentzenchr!

I clicked on "Update branch" because it looks like the CI was in a weird state.

The fix looks fine at a glance, I'll take a closer look at this today, push a few tweaks and merge it.

@lesteve lesteve added this to the 1.8 milestone Nov 20, 2025
Comment thread sklearn/linear_model/_logistic.py Resolved
coefs = clf_multi.coefs_paths_[fold, 0, :, :, :-1]
coefs = coefs.reshape(len(clf_multi.Cs_), -1)
norms = np.sum(coefs * coefs, axis=1) # L2 norm for each C
norms = np.sum(coefs * coefs, axis=(-2, -1)) # L2 norm for each C

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to our future selves, this is a left-over from #32073 (review). I merged #32073 too soon and forgot about it ...

@lorentzenchr

Copy link
Copy Markdown
Member Author

If CD still green, ready from my site.

@lorentzenchr

Copy link
Copy Markdown
Member Author

Preferably: first merge #32659 then this one.

@lesteve

lesteve commented Nov 21, 2025

Copy link
Copy Markdown
Member

I am going to merge this one first, since I feel it is ready.

The conflicts are going to be painful to solve whichever we merged first I think ...

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

Projects

None yet

3 participants