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

Batch Shuffling & Mini-Batch Generator

Data Processing
Medium

Shuffle matching feature and label arrays once, then yield consecutive mini-batches. Use np.random.default_rng(seed) to shuffle one index array and apply it to both inputs. Each yield must be a tuple (X_batch, y_batch) containing NumPy arrays. If drop_last is true, omit a final batch smaller than batch_size.

Loading visualization...

Examples

Input: X = [0, 1, 2, 3, 4, 5, 6], y = [0, 1, 2, 3, 4, 5, 6], batch_size = 3, seed = 42, drop_last = False

Output: [[[3, 2, 6], [3, 2, 6]], [[4, 1, 5], [4, 1, 5]], [[0], [0]]]

Explanation: One seeded permutation is sliced into batches of three, with the final single sample retained.

Input: X = [0, 1, 2, 3, 4, 5, 6], y = [0, 1, 2, 3, 4, 5, 6], batch_size = 3, seed = 42, drop_last = True

Output: [[[3, 2, 6], [3, 2, 6]], [[4, 1, 5], [4, 1, 5]]]

Hint 1

Shuffle np.arange(len(X)) with np.random.default_rng(seed).

Hint 2

Slice the shuffled indices inside range(0, len(indices), batch_size).

Requirements

  • Return a Python generator
  • Apply one seeded permutation to both X and y
  • Yield tuples of NumPy arrays
  • Respect drop_last without modifying the inputs

Constraints

  • X and y contain the same positive number of samples
  • batch_size is a positive integer
  • Use NumPy only
Try Similar Problems
Kfold SplitStratified SplitPad SequencesImpute MissingDropout Training

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Batch Shuffling & Mini-Batch Generator

Data Processing
Medium

Shuffle matching feature and label arrays once, then yield consecutive mini-batches. Use np.random.default_rng(seed) to shuffle one index array and apply it to both inputs. Each yield must be a tuple (X_batch, y_batch) containing NumPy arrays. If drop_last is true, omit a final batch smaller than batch_size.

Loading visualization...

Examples

Input: X = [0, 1, 2, 3, 4, 5, 6], y = [0, 1, 2, 3, 4, 5, 6], batch_size = 3, seed = 42, drop_last = False

Output: [[[3, 2, 6], [3, 2, 6]], [[4, 1, 5], [4, 1, 5]], [[0], [0]]]

Explanation: One seeded permutation is sliced into batches of three, with the final single sample retained.

Input: X = [0, 1, 2, 3, 4, 5, 6], y = [0, 1, 2, 3, 4, 5, 6], batch_size = 3, seed = 42, drop_last = True

Output: [[[3, 2, 6], [3, 2, 6]], [[4, 1, 5], [4, 1, 5]]]

Hint 1

Shuffle np.arange(len(X)) with np.random.default_rng(seed).

Hint 2

Slice the shuffled indices inside range(0, len(indices), batch_size).

Requirements

  • Return a Python generator
  • Apply one seeded permutation to both X and y
  • Yield tuples of NumPy arrays
  • Respect drop_last without modifying the inputs

Constraints

  • X and y contain the same positive number of samples
  • batch_size is a positive integer
  • Use NumPy only
Try Similar Problems
Kfold SplitStratified SplitPad SequencesImpute MissingDropout Training

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.