[Python] Sklearn and cross_val_scores: can we use it for leave-one-out cross validation?

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a loop where I have to execute the k-fold cross-validation on 1000 entries. The value of k changes at every iteration in the loop and it can have the value of 1000. However, when trying to do the leave-on-out cross-validation (i.e. k = #entries = 1000) I receive the following error:

    ValueError: All the n_groups for individual classes are less than n_splits=1000.

    test = [...,...,1000] for n in test:

    knn = KNeighborsClassifier(n_neighbors=1)
    scores = cross_val_score(knn, X_train, y_train, cv=n, scoring='accuracy')


    Remember that X_train and y_train have 1000 rows and are numpy arrays. I know there are specific methods for the LOO but I am wondering whether there could be a way to use the same function in order to avoid inserting an if in the loop.

    Continue reading...

Compartilhe esta Página