[Python] Error on CrossValidation with classes that have less samples than number of splits

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently working on this issue: https://github.com/scikit-learn/scikit-learn/issues/30832

    I am studying possible approaches to fix it.

    Explanation:

    When using Logistic Regression and Cross-Validation, classes with less samples than number of splits can't have at least a sample per fold on Cross Validation, leading to an error when executing the python program. Even though this might be a data problem, there should be better ways to deal with it than the error there is right now.

    Code to replicate the bug: from sklearn.linear_model import LogisticRegressionCV import numpy as np n, m = 20, 5 X = np.random.randn(n, m) y = np.random.randint(0, 2, n) y[-3:] = [3, 4, 5] LogisticRegressionCV().fit(X, y)

    Some approaches I thought of:

    • Setting the coefficients to 0 in underrepresented classes;
    • Automatically creating new data based on the real data we have and it's patterns (even if it's only 1 sample);
    • Duplicating the data until the class with the lowest samples reaches the number of splits;
    • Simply raising a more meaningful exception.

    Is this simply a data problem and I should just raise an exception or can I do something more here? Can you give me some tips or thoughts about what is a good way to solve this issue?

    Continue reading...

Compartilhe esta Página