Skip to content

LogisticRegressionCV bug when one fold has not all classes #32748

Closed

Description

@lesteve

I may be missing something but it seems like the coefficient for a given class does not stay at zero when a class is missing.

I took the Iris dataset with the well known issue that y is ordered with 3 classes so that if you use cv=KFold(3) you will get the issue that each training fold will have 2 of the 3 classes, and the the test fold has the third class.

import numpy as np

from sklearn.linear_model import LogisticRegressionCV
from sklearn.datasets import load_iris
from sklearn.model_selection import KFold

X, y = load_iris(return_X_y=True)

# Error occurs on `main` but on on this branch
lr = LogisticRegressionCV(cv=KFold(3), Cs=[1], fit_intercept=False).fit(X, y)
lr.coefs_paths_

As far as I can see there are no zero-coefficient anywhere?

{np.int64(0): array([[[ 0.36617189,  0.4181025 , -1.80054529, -1.59885258,
          11.94538259]],
 
        [[-0.27013869,  0.19659815, -1.0394569 , -0.4717853 ,
           8.11303679]],
 
        [[-0.2259761 ,  0.55491805, -1.32935417, -0.56475712,
           6.7920903 ]]]),
 np.int64(1): array([[[-0.36863165, -0.41910848,  1.80385762,  1.60144714,
          -4.03491417]],
 
        [[ 0.26501596, -0.1994853 ,  1.03704383,  0.47085702,
          -1.69095992]],
 
        [[ 0.22447638, -0.55428594,  1.33474803,  0.56733333,
          -0.29154238]]]),
 np.int64(2): array([[[ 2.45975917e-03,  1.00598025e-03, -3.31232754e-03,
          -2.59456480e-03, -7.91046843e+00]],
 
        [[ 5.12273225e-03,  2.88714428e-03,  2.41307063e-03,
           9.28278209e-04, -6.42207687e+00]],
 
        [[ 1.49971712e-03, -6.32115481e-04, -5.39386365e-03,
          -2.57621573e-03, -6.50054792e+00]]])}

For completeness, the same snippet fails on main so I guess this could be turned into a test for the new behaviour if it becomes clearer what to test exactly (maybe a smoke test is enough?) ...

ValueError: cannot reshape array of size 15 into shape (3,1,3,newaxis)

Warning

@lesteve spotted a hickup (call it bug) that needs fixing.

Checking the assumptions

Smuggling in

c, f = np.unique(y_train, return_counts=True)
cf = {int(ci):int(fi) for ci, fi in zip(c, f)}
print(f"y_train counts={cf}")

right before coefs, Cs, n_iter = _logistic_regression_path( around line 683 gives

y_train counts={1: 50, 2: 50}
y_train counts={0: 50, 2: 50}
y_train counts={0: 50, 1: 50}

This confirms: Class i is missing in fold i.

Why are the coefficients of all classes changed?

Because they enter the objective function via the penalty. Therefore, the above code comment should be modified.

Why there is a bug to fix?

Inside function _logistic_regression_path, around line 280

le = LabelEncoder()

This should instead read

le = LabelEncoder().fit(classes)

and the subsequent fit_transform should be changed to transform (this fixes a bug and makes it faster).

Remaining tasks

Even if this is fixed, there remains a bug if one passes another scorer, e.g.

lr = LogisticRegressionCV(cv=KFold(3), Cs=[1], fit_intercept=False,scoring="neg_brier_score").fit(X, y)
ValueError: y_true contains only one label (0). Please provide the list of all expected class labels explicitly through the labels argument.

I would postpone fixing this to another future PR, maybe not part of 1.8. The situation is already better if the above is fixed.

Originally posted by @lorentzenchr in #32073 (comment)

Activity

  1. added
    Needs Triage
    on Nov 20, 2025
  2. added this to the 1.8 milestone on Nov 20, 2025
  3. added and removed
    Needs Triage
    on Nov 20, 2025
  4. lesteve commented on Nov 20, 2025

    @lesteve
    MemberAuthor

    I opened #32749 with @lorentzenchr's suggestion of using le.fit(classes) + le.transform instead of le.fit_transform.

    Actually there was already an open PR: #32747

  5. removed this from the 1.8 milestone on Nov 20, 2025
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions