Stratified Sample Allocation
Implement stratified_sample_counts(class_counts, total_samples) that returns an integer NumPy array of per-class sample counts summing to total_samples.
Examples
Equal classes: proportional allocation divides evenly
- Input
- stratified_sample_counts([100, 100], 10)
- Output
- [5, 5]
80/20 population split: allocation matches proportions exactly
- Input
- stratified_sample_counts([80, 20], 10)
- Output
- [8, 2]
1/6 remainder assigned to smallest class by largest fractional part
- Input
- stratified_sample_counts([100, 200, 300], 10)
- Output
- [2, 3, 5]
Hints
Hint 1
You need the index of the extreme value, not the value itself.
Hint 2
Reach for largest remainder method rather than round.
Requirements
class_counts: 1D array of per-class population sizes, shape (k,)total_samples: Total number of samples to allocateReturn Integer array of shape (k,) summing to total_samples.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
6 employers weight this skill
3 quant funds, 2 big tech firms, 1 health and bio company. Top match scores 71.
import numpy as np
def stratified_sample_counts(class_counts: np.ndarray, total_samples: int) -> np.ndarray:
"""
Compute per-class sample counts via stratified sampling (largest remainder method).
Args:
class_counts: 1D array of per-class population sizes, shape (k,)
total_samples: Total number of samples to allocate
Returns:
Integer array of shape (k,) summing to total_samples.
"""
# YOUR CODE HERE
pass