Repository navigation
Closed
Labels
Description
Describe the bug
The code provided below raises ValueError. I guess that the problem is that minor classes may not be included in train or val sets for some folds during internal cross-validation, even with stratified split. This produces errors with some metrics other than default (accuracy).
One solution may be setting log-proba to -inf for classes not present in the train set, as well as providing label argument. How can I fix this in the most simple way?
Steps/Code to Reproduce
import numpy as np
from sklearn.linear_model import LogisticRegressionCV
X = np.zeros((10, 1))
y = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3]
logreg = LogisticRegressionCV(cv=5, scoring='neg_log_loss')
logreg.fit(X, y)
Expected Results
No exception thrown
Actual Results
ValueError: y_true and y_pred contain different number of classes 2, 3. Please provide the true labels explicitly through the labels argument. Classes found in y_true: [0 1]
Versions
System:
python: 3.10.8 (main, Nov 2 2023, 15:57:09) [GCC 9.4.0]
executable: /data/osedukhin/tabular-models/venv/bin/python
machine: Linux-5.4.0-123-generic-x86_64-with-glibc2.31
Python dependencies:
sklearn: 1.4.0
pip: 22.2.2
setuptools: 63.2.0
numpy: 1.26.2
scipy: 1.11.3
Cython: None
pandas: 2.1.3
matplotlib: 3.8.1
joblib: 1.3.2
threadpoolctl: 3.2.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 32
prefix: libopenblas
filepath: /data/osedukhin/tabular-models/venv/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-0cf96a72.3.23.dev.so
version: 0.3.23.dev
threading_layer: pthreads
architecture: SkylakeX
user_api: openmp
internal_api: openmp
num_threads: 32
prefix: libgomp
filepath: /data/osedukhin/tabular-models/venv/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
version: None
user_api: blas
internal_api: openblas
num_threads: 32
prefix: libopenblas
filepath: /data/osedukhin/tabular-models/venv/lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-23e5df77.3.21.dev.so
version: 0.3.21.dev
threading_layer: pthreads
architecture: SkylakeX
Activity
sedol1339 commentedon Jan 18, 2024
Found the same issue some years ago: #15389
sedol1339 commentedon Jan 18, 2024
@glemaitre mentioned that this can be fixed with:
Does labels order matter? For example, if y = [2, 2, 1, 1, 0, 0], then will passing labels=[0, 1, 2] and labels=[2, 1, 0] work correctly?
sedol1339 commentedon Jan 18, 2024
log_loss documentation says that "The labels in y_pred are assumed to be ordered alphabetically, as done by LabelBinarizer". Does this mean lexicographical sorting, even for integer labels? For example, is [0, 10, 9] a correct sorting?
glemaitre commentedon Jan 18, 2024
The issue is not the
log_lossbut rather theStratifiedKFold. Bascially on your providedy, we would get the following split:All 4 first splits get all the classes at fit so the different
LogisticRegressionmodels knows that the classes are[1, 2, 3]. For the last split, the only sample of class3is kept for the test split. Therefore, the models are trained on two classes and thus the output of thepredict_probawill be a(n_samples_test, 2)matrix instead of(n_samples, 3).So I am not sure that we can do much here because this is a really ill-posed problem.
sedol1339 commentedon Jan 19, 2024
@glemaitre is it possible to append zero output probabilities for classes not present in the train?
glemaitre commentedon Jan 19, 2024
I don't see an easy way to do so without being a hack.
lorentzenchr commentedon Jul 29, 2025
I close as it is a duplicate of #15389.