Stratified K-Fold for Regression Targets

Quick Overview

Implement stratified k-fold cross-validation for a continuous regression target by cutting it into rank-based quantile bins and dealing each bin round-robin across folds. It tests stable ranking with ties, integer bin arithmetic, input validation and why a regression target needs stratified folds.

Stratified K-Fold for Regression Targets

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Given a 1-D continuous target `y`, bin it into `n_bins` rank-based quantile bins, then assign samples to `n_splits` validation folds so that every bin is spread evenly across the folds. Implement the three functions below. - `quantile_bin_labels(y, n_bins)`: rank the samples with a stable ascending sort (ties broken by original index) and set `bin = rank * n_bins // n`. Return an `int64` array of shape `(n,)`. - `stratified_regression_fold_ids(y, n_splits, n_bins)`: within each bin, the members in ascending index order get fold ids `0, 1, ..., n_splits-1, 0, ...` (round-robin). - `stratified_regression_split(y, n_splits, n_bins)`: return a list of `n_splits` tuples `(train_idx, val_idx)`, fold `k` at position `k`, with both arrays ascending `int64`. - Raise `ValueError` if `y` is not 1-D, if `n_bins` is outside `[1, n]`, or if `n_splits < 2`. - Both the fold-id and split functions raise `ValueError` if any bin has fewer than `n_splits` samples. Example: ```python y = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] quantile_bin_labels(y, 3) # [2, 2, 2, 1, 1, 1, 0, 0, 0, 0] stratified_regression_fold_ids(y, n_splits=3, n_bins=3) # [0, 1, 2, 0, 1, 2, 0, 1, 2, 0] ``` The values run in descending order, so index 9 has rank 0 and index 0 has rank 9. Ranks 0 to 3 fall in bin 0, ranks 4 to 6 in bin 1 and ranks 7 to 9 in bin 2. ```hint From order to rank A stable argsort tells you which sample sits at each sorted position. The rank you need is the other direction: the sorted position of each sample. ``` ```hint Validation Bin sizes can be checked in one pass with a count per bin, before any fold ids are assigned. ``` ### Constraints & Assumptions - `n` is between 1 and 10,000, and the values in `y` are finite floats (ties allowed). - Use NumPy only. The output must be deterministic. ### What a Strong Answer Covers - Stable ranks with ties broken by original index, and the bin formula computed with integer arithmetic. - Round-robin fold ids within each bin, dealt in ascending index order. - Every `ValueError` check, including the minimum bin size against `n_splits`. - Split output in fold order with sorted `int64` train and validation arrays. - Why rank-based bins have sizes within one of each other, and why the size check makes every fold cover every bin. ### Follow-up Questions - How would you add shuffling with a random seed? - Why use rank-based bins instead of `np.quantile` edges? - How would you handle a bin that is too small instead of raising? - Fold 0 receives the leftover samples from every bin. How large can the fold-size gap get, and how would you remove it?

Overview: Implement stratified k-fold cross-validation for a continuous regression target by cutting it into rank-based quantile bins and dealing each bin round-robin across folds. It tests stable ranking with ties, integer bin arithmetic, input validation and why a regression target needs stratified folds.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Given a 1-D continuous target y, bin it into n_bins rank-based quantile bins, then assign samples to n_splits validation folds so that every bin is spread evenly across the folds. Implement the three functions below.

  • quantile_bin_labels(y, n_bins) : rank the samples with a stable ascending sort (ties broken by original index) and set bin = rank * n_bins // n . Return an int64 array of shape (n,) .
  • stratified_regression_fold_ids(y, n_splits, n_bins) : within each bin, the members in ascending index order get fold ids 0, 1, ..., n_splits-1, 0, ... (round-robin).
  • stratified_regression_split(y, n_splits, n_bins) : return a list of n_splits tuples (train_idx, val_idx) , fold k at position k , with both arrays ascending int64 .
  • Raise ValueError if y is not 1-D, if n_bins is outside [1, n] , or if n_splits < 2 .
  • Both the fold-id and split functions raise ValueError if any bin has fewer than n_splits samples.

Example:

y = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
quantile_bin_labels(y, 3)
# [2, 2, 2, 1, 1, 1, 0, 0, 0, 0]
stratified_regression_fold_ids(y, n_splits=3, n_bins=3)
# [0, 1, 2, 0, 1, 2, 0, 1, 2, 0]

The values run in descending order, so index 9 has rank 0 and index 0 has rank 9. Ranks 0 to 3 fall in bin 0, ranks 4 to 6 in bin 1 and ranks 7 to 9 in bin 2.

Constraints & Assumptions

  • n is between 1 and 10,000, and the values in y are finite floats (ties allowed).
  • Use NumPy only. The output must be deterministic.

What a Strong Answer Covers Guidance

  • Stable ranks with ties broken by original index, and the bin formula computed with integer arithmetic.
  • Round-robin fold ids within each bin, dealt in ascending index order.
  • Every ValueError check, including the minimum bin size against n_splits .
  • Split output in fold order with sorted int64 train and validation arrays.
  • Why rank-based bins have sizes within one of each other, and why the size check makes every fold cover every bin.

Follow-up Questions Guidance

  • How would you add shuffling with a random seed?
  • Why use rank-based bins instead of np.quantile edges?
  • How would you handle a bin that is too small instead of raising?
  • Fold 0 receives the leftover samples from every bin. How large can the fold-size gap get, and how would you remove it?