Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Cross-Validation in Python

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Cross-Validation in Python. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

H2O-3 evaluates models across multiple folds to estimate generalization performance. Implement the split-generation portion of k-fold cross-validation without using machine learning libraries.

Given a feature matrix X, label array y, number of folds k, and optional shuffling parameters, return the train and validation indices for every fold. Each sample must appear in exactly one validation set, while every training set contains all samples not assigned to that fold.

Formal Specification

Implement cross_validate(X, y, k, shuffle, seed):

  • X is a list of feature rows. Its values are not used when creating indices.
  • y is a list of labels with the same length as X.
  • k is an integer between 2 and len(X).
  • shuffle is a Boolean. If false, preserve the original index order. If true, shuffle indices using seed.
  • seed is an integer or None.
  • Return a list of k dictionaries. Each dictionary must contain train, a list of indices, and validation, a list of indices.

Validation folds should be as balanced as possible. Earlier folds receive one extra sample when the dataset size is not divisible by k. Do not mutate X or y.

Constraints

  • 2 <= len(X) == len(y) <= 10^5
  • 2 <= k <= len(X)
  • Fold sizes differ by at most one sample
  • Each index appears in exactly one validation fold
  • X and y must not be mutated

Function Signature

def cross_validate(X, y, k, shuffle, seed):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output