Mini-Batch Processing
~12 mincode completion
Implement make_batches(X, batch_size) that returns a list of NumPy arrays, one per batch.
Examples
5 elements with batch_size=2 produces 3 batches
- Input
- make_batches([1, 2, 3, 4, 5], 2)
- length of result
- 3
First batch contains first two elements
- Input
- make_batches([10, 20, 30, 40], 2)
- [0] of result
- [10, 20]
Evenly divisible: no partial batch at end
- Input
- make_batches([1, 2, 3, 4, 5, 6], 3)
- length of result
- 2
Hints
Hint 1
A list comprehension expresses this in one line.
Hint 2
Watch for this: dropped final incomplete batch.
Requirements
X: Array of shape (n ...), may be 1D or 2Dbatch_size: Number of samples per batchReturn List of NumPy arrays, each of size <= batch_size along axis 0.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~12 min
••••••••••••••••
8 employers weight this skill
4 big tech firms, 1 AI product company, 1 enterprise vendor, 1 defense company, 1 quant fund. Top match scores 93.
Python
import numpy as np
def make_batches(X: np.ndarray, batch_size: int) -> list:
"""
Split X into consecutive mini-batches of size batch_size.
The final batch may be smaller if n is not divisible by batch_size.
Args:
X: Array of shape (n ...), may be 1D or 2D
batch_size: Number of samples per batch
Returns:
List of NumPy arrays, each of size <= batch_size along axis 0.
"""
# YOUR CODE HERE
pass