Ensemble MethodsEasy
Bootstrap Sampling
~15 mincode completion
Implement bootstrap_sample(X, y, seed) that returns (X_sample, y_sample), bootstrapped versions of the data using the given random seed.
Examples
Bootstrapped y_sample for seed 42
- Input
- bootstrap_sample([[1, 2], [3, 4], [5, 6], [7, 8]], [0, 1, 0, 1], 42)
- [1] of result
- [0, 1, 0, 1]
Bootstrapped y_sample for seed 0
- Input
- bootstrap_sample([[1], [2], [3], [4], [5]], [1, 2, 3, 4, 5], 0)
- [1] of result
- [5, 4, 3, 2, 2]
Seed=0 produces specific first label
- Input
- bootstrap_sample([[1], [2], [3], [4], [5]], [10, 20, 30, 40, 50], 0)
- [1][0] of result
- 50
Hints
Hint 1
Work directly with the arguments X, y, and return the result rather than printing it.
Hint 2
Watch for this: used replace false.
Requirements
X: Feature matrix of shape (n, d)y: Label vector of shape (n,): Random seed for reproducibility
Return Tuple (X_sample, y_sample) each of shape (n, d) and (n,).
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~15 min
••••••••••••••••
8 employers weight this skill
2 big tech firms, 2 health and bio companies, 2 quant funds, 1 AI product company, 1 enterprise vendor. Top match scores 93.
Python
import numpy as np
def bootstrap_sample(X: np.ndarray, y: np.ndarray, seed: int):
"""
Draw a bootstrap sample (with replacement) of the full dataset.
Args:
X: Feature matrix of shape (n, d)
y: Label vector of shape (n,)
seed: Random seed for reproducibility
Returns:
Tuple (X_sample, y_sample) each of shape (n, d) and (n,).
"""
# YOUR CODE HERE
pass