juaml / juaml/julearn

[ENH]: Implement a Repetead Group K Fold CV

Open
#281 0 comments 0 reactions 1 assignee View on GitHub

@fraimondo is already working on this.

Since Mar 18, 2025.

enhancement
Dominant language
Python
Stars
33
Forks
19
PR merge metrics
No merged PRs in 30d

Description

Which feature do you want to include?

We need a repeated (non-deterministic) group k-fold

How do you imagine this integrated in julearn?

Something like this, from @kaurao using ChatGPT

import numpy as np
from sklearn.model_selection import GroupKFold

class RepeatedGroupKFold:
    def __init__(self, n_splits=5, n_repeats=5, random_state=None):
        self.n_splits = n_splits
        self.n_repeats = n_repeats
        self.random_state = np.random.RandomState(random_state)

    def split(self, X, y=None, groups=None):
        if groups is None:
            raise ValueError("Groups must be provided for GroupKFold.")

        unique_groups = np.unique(groups)

        for repeat in range(self.n_repeats):
            # Shuffle groups before each repeat
            shuffled_groups = self.random_state.permutation(unique_groups)

            folds = np.array_split(shuffled_groups, self.n_splits)

            for fold_groups in folds:
                test_idx = np.isin(groups, fold_groups)
                train_idx = ~test_idx

                yield np.where(train_idx)[0], np.where(test_idx)[0]

    def get_n_splits(self, X=None, y=None, groups=None):
        return self.n_splits * self.n_repeats
Do you have a sample code that implements this outside of julearn?

Anything else to say?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.