Repeated K-Fold With Per-Repetition Seeds

Quick Overview

Implement repeated K-fold cross-validation where each repetition draws its own seeded permutation, plus a function that returns every sample's fold id in each repetition. It tests fold sizing, reproducible seeding, repetition-major ordering and index bookkeeping with NumPy.

Repeated K-Fold With Per-Repetition Seeds

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `repeated_kfold_splits(n_samples, n_splits, n_repeats, random_state)`, returning a list of `n_repeats * n_splits` `(train_idx, val_idx)` pairs ordered repetition-major (all folds of repetition 0, then repetition 1, and so on). Also implement `repeated_fold_assignments(...)` with the same arguments, returning an `(n_repeats, n_samples)` `int64` array where entry `[r, i]` is the fold id of sample `i` in repetition `r`. The functions must follow these rules: - Repetition `r` uses `perm = np.random.default_rng(random_state + r).permutation(n_samples)`. - Fold sizes are `n_samples // n_splits`, with the first `n_samples % n_splits` folds getting one extra sample. - Fold `f`'s validation set is the next contiguous slice of `perm` (in fold order), sorted ascending. - The training set is the sorted complement of the validation set. Both are `int64` arrays. - Raise `ValueError` if `n_splits < 2`, `n_splits > n_samples`, or `n_repeats < 1`. Example: ```python splits = repeated_kfold_splits(13, 4, 3, 0) len(splits) # 12 [len(v) for _, v in splits[:4]] # [4, 3, 3, 3] (same for every repetition) ``` ```hint Fold ids from the splits The second function does not need its own fold logic. Look at how a split's position in the repetition-major list encodes its repetition and its fold. ``` ### Constraints & Assumptions - `random_state` is a non-negative integer. - The same arguments must always produce identical output. ### What a Strong Answer Covers - Argument validation that raises `ValueError` for each listed case. - Fold sizes with the remainder on the first folds, and validation slices taken from the permutation in fold order. - A generator seeded with `random_state + r` for each repetition, and why that makes each repetition reproducible on its own. - Repetition-major ordering, and a fold-assignment array in which every sample gets exactly one fold id per repetition. - `int64` outputs and the time and memory cost of storing every split. ### Follow-up Questions - How would you make this stratified by class label? - Why derive a distinct seed per repetition instead of one shared RNG?

Overview: Implement repeated K-fold cross-validation where each repetition draws its own seeded permutation, plus a function that returns every sample's fold id in each repetition. It tests fold sizing, reproducible seeding, repetition-major ordering and index bookkeeping with NumPy.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement repeated_kfold_splits(n_samples, n_splits, n_repeats, random_state), returning a list of n_repeats * n_splits (train_idx, val_idx) pairs ordered repetition-major (all folds of repetition 0, then repetition 1, and so on). Also implement repeated_fold_assignments(...) with the same arguments, returning an (n_repeats, n_samples) int64 array where entry [r, i] is the fold id of sample i in repetition r.

The functions must follow these rules:

  • Repetition r uses perm = np.random.default_rng(random_state + r).permutation(n_samples) .
  • Fold sizes are n_samples // n_splits , with the first n_samples % n_splits folds getting one extra sample.
  • Fold f 's validation set is the next contiguous slice of perm (in fold order), sorted ascending.
  • The training set is the sorted complement of the validation set. Both are int64 arrays.
  • Raise ValueError if n_splits < 2 , n_splits > n_samples , or n_repeats < 1 .

Example:

splits = repeated_kfold_splits(13, 4, 3, 0)
len(splits)                       # 12
[len(v) for _, v in splits[:4]]   # [4, 3, 3, 3] (same for every repetition)

Constraints & Assumptions

  • random_state is a non-negative integer.
  • The same arguments must always produce identical output.

What a Strong Answer Covers Guidance

  • Argument validation that raises ValueError for each listed case.
  • Fold sizes with the remainder on the first folds, and validation slices taken from the permutation in fold order.
  • A generator seeded with random_state + r for each repetition, and why that makes each repetition reproducible on its own.
  • Repetition-major ordering, and a fold-assignment array in which every sample gets exactly one fold id per repetition.
  • int64 outputs and the time and memory cost of storing every split.

Follow-up Questions Guidance

  • How would you make this stratified by class label?
  • Why derive a distinct seed per repetition instead of one shared RNG?