Implement a Leave-One-Group-Out Cross-Validation Splitter

Quick Overview

Implement a leave-one-group-out cross-validation splitter in Python that yields sorted train and test index arrays for each unique group and counts the splits. It tests group-aware validation, NumPy masking, input checks and how generator methods report errors.

Implement a Leave-One-Group-Out Cross-Validation Splitter

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement a `LeaveOneGroupOut` class with a generator method `split(X, y=None, groups=None)` that yields one `(train_idx, test_idx)` pair per unique group, holding that group out as the test fold. Also implement `get_n_splits(X=None, y=None, groups=None)`, which returns the number of unique groups as an `int`. Requirements: - Iterate over the groups in sorted order (the order `np.unique` returns), matching scikit-learn's `LeaveOneGroupOut`. - Train and test are sorted integer index arrays that together cover all samples with no overlap. - `groups` may be numeric or strings. - Both methods raise `ValueError` when `groups is None` or when there are fewer than two distinct groups. - Raise `ValueError` when `groups` is not 1-D, or, in `split`, when its length differs from `len(X)`. Example: ```python groups = [3, 1, 3, 2, 1, 2, 3] # split yields: # train=[0, 2, 3, 5, 6], test=[1, 4] (group 1) # train=[0, 1, 2, 4, 6], test=[3, 5] (group 2) # train=[1, 3, 4, 5], test=[0, 2, 6] (group 3) # get_n_splits(groups=groups) == 3 ``` ```hint Splitting the indices A boolean array over the samples can index `np.arange(n)` directly, and both the selected and the unselected positions stay in ascending order. ``` ### What a Strong Answer Covers - Group iteration in the same sorted order as `np.unique`, for numeric and string groups. - Train and test arrays that are sorted, disjoint and together cover every sample. - All the `ValueError` cases, shared between the two methods so they cannot drift apart. - When the validation in a generator method actually runs. - The time and memory cost per split. ### Follow-up Questions - With thousands of groups, how would you avoid building a full boolean mask for every group? - How does this splitter differ from `GroupKFold`, and when would you pick `GroupKFold` instead? - If group sizes are very uneven, what does that do to the cross-validation estimate, and how would you report the result?

Overview: Implement a leave-one-group-out cross-validation splitter in Python that yields sorted train and test index arrays for each unique group and counts the splits. It tests group-aware validation, NumPy masking, input checks and how generator methods report errors.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement a LeaveOneGroupOut class with a generator method split(X, y=None, groups=None) that yields one (train_idx, test_idx) pair per unique group, holding that group out as the test fold. Also implement get_n_splits(X=None, y=None, groups=None), which returns the number of unique groups as an int.

Requirements:

  • Iterate over the groups in sorted order (the order np.unique returns), matching scikit-learn's LeaveOneGroupOut .
  • Train and test are sorted integer index arrays that together cover all samples with no overlap.
  • groups may be numeric or strings.
  • Both methods raise ValueError when groups is None or when there are fewer than two distinct groups.
  • Raise ValueError when groups is not 1-D, or, in split , when its length differs from len(X) .

Example:

groups = [3, 1, 3, 2, 1, 2, 3]
# split yields:
# train=[0, 2, 3, 5, 6], test=[1, 4]     (group 1)
# train=[0, 1, 2, 4, 6], test=[3, 5]     (group 2)
# train=[1, 3, 4, 5],    test=[0, 2, 6]  (group 3)
# get_n_splits(groups=groups) == 3

What a Strong Answer Covers Guidance

  • Group iteration in the same sorted order as np.unique , for numeric and string groups.
  • Train and test arrays that are sorted, disjoint and together cover every sample.
  • All the ValueError cases, shared between the two methods so they cannot drift apart.
  • When the validation in a generator method actually runs.
  • The time and memory cost per split.

Follow-up Questions Guidance

  • With thousands of groups, how would you avoid building a full boolean mask for every group?
  • How does this splitter differ from GroupKFold , and when would you pick GroupKFold instead?
  • If group sizes are very uneven, what does that do to the cross-validation estimate, and how would you report the result?