TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

K-Fold Split (Indices Only)

Data Processing
Hard

Partition sample indices from zero through N−1N-1N−1 into kkk folds for cross-validation. When NNN is not divisible by kkk, the first N mod kN\bmod kNmodk validation folds receive one extra index.

{0,1,…,N−1}=F1∪F2∪⋯∪Fk\{0,1,\ldots,N-1\}=F_1\cup F_2\cup\cdots\cup F_k{0,1,…,N−1}=F1​∪F2​∪⋯∪Fk​ Fi∩Fj=∅for i≠jF_i\cap F_j=\varnothing\quad\text{for }i\ne jFi​∩Fj​=∅for i=j

For fold iii, validation indices are FiF_iFi​ and training indices are all remaining folds. If shuffle is true, shuffle indices with np.random.default_rng(seed).permutation before partitioning. Return a list of kkk dictionaries, each containing train_idx and val_idx as one-dimensional integer NumPy arrays.

Loading visualization...

Examples

Input: N = 5, k = 2, shuffle = false, seed = 0

Output: [{"train_idx": [3, 4], "val_idx": [0, 1, 2]}, {"train_idx": [0, 1, 2], "val_idx": [3, 4]}]

Explanation: Five indices produce validation folds of sizes three and two, and each fold is held out once.

Input: N = 7, k = 3, shuffle = false, seed = 0

Output: [{"train_idx": [3, 4, 5, 6], "val_idx": [0, 1, 2]}, {"train_idx": [0, 1, 2, 5, 6], "val_idx": [3, 4]}, {"train_idx": [0, 1, 2, 3, 4], "val_idx": [5, 6]}]

Hint 1

Use np.array_split(indices, k) to create balanced validation folds.

Hint 2

Concatenate every fold except the current validation fold for train_idx.

Requirements

  • Place every index in exactly one validation fold
  • Keep validation fold sizes within one element of each other
  • Use the supplied seed when shuffling
  • Return a list of dictionaries containing train_idx and val_idx arrays

Constraints

  • 2 <= k <= N
  • seed is a nonnegative integer
  • Use NumPy only
Try Similar Problems
Stratified SplitImpute MissingBatch GeneratorClassification MetricsCross Entropy Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: number

Accepts: number

Accepts: number

You must run your code first.
PrevNext

K-Fold Split (Indices Only)

Data Processing
Hard

Partition sample indices from zero through N−1N-1N−1 into kkk folds for cross-validation. When NNN is not divisible by kkk, the first N mod kN\bmod kNmodk validation folds receive one extra index.

{0,1,…,N−1}=F1∪F2∪⋯∪Fk\{0,1,\ldots,N-1\}=F_1\cup F_2\cup\cdots\cup F_k{0,1,…,N−1}=F1​∪F2​∪⋯∪Fk​ Fi∩Fj=∅for i≠jF_i\cap F_j=\varnothing\quad\text{for }i\ne jFi​∩Fj​=∅for i=j

For fold iii, validation indices are FiF_iFi​ and training indices are all remaining folds. If shuffle is true, shuffle indices with np.random.default_rng(seed).permutation before partitioning. Return a list of kkk dictionaries, each containing train_idx and val_idx as one-dimensional integer NumPy arrays.

Loading visualization...

Examples

Input: N = 5, k = 2, shuffle = false, seed = 0

Output: [{"train_idx": [3, 4], "val_idx": [0, 1, 2]}, {"train_idx": [0, 1, 2], "val_idx": [3, 4]}]

Explanation: Five indices produce validation folds of sizes three and two, and each fold is held out once.

Input: N = 7, k = 3, shuffle = false, seed = 0

Output: [{"train_idx": [3, 4, 5, 6], "val_idx": [0, 1, 2]}, {"train_idx": [0, 1, 2, 5, 6], "val_idx": [3, 4]}, {"train_idx": [0, 1, 2, 3, 4], "val_idx": [5, 6]}]

Hint 1

Use np.array_split(indices, k) to create balanced validation folds.

Hint 2

Concatenate every fold except the current validation fold for train_idx.

Requirements

  • Place every index in exactly one validation fold
  • Keep validation fold sizes within one element of each other
  • Use the supplied seed when shuffling
  • Return a list of dictionaries containing train_idx and val_idx arrays

Constraints

  • 2 <= k <= N
  • seed is a nonnegative integer
  • Use NumPy only
Try Similar Problems
Stratified SplitImpute MissingBatch GeneratorClassification MetricsCross Entropy Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: number

Accepts: number

Accepts: number

You must run your code first.