Ensemble MethodsEasy
Majority Vote
~15 mincode completion
Implement majority_vote(predictions) that returns a 1D array of final class predictions.
Examples
3 models, clear majority
- Input
- majority_vote([[0, 1, 1], [1, 0, 1], [0, 0, 1]])
- Output
- [1, 1, 0]
All models agree
- Input
- majority_vote([[1, 1, 1], [0, 0, 0]])
- Output
- [1, 0]
5 models, 3-2 split
- Input
- majority_vote([[0, 0, 0, 1, 1], [1, 1, 1, 0, 0]])
- Output
- [0, 1]
Hints
Hint 1
You need the index of the extreme value, not the value itself.
Hint 2
A common slip here: summed instead of mode.
Requirements
predictions: Integer array of shape (n_samples, n_models)Return 1D integer array of shape (n_samples,) with the most common class per row.
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 majority_vote(predictions: np.ndarray) -> np.ndarray:
"""
Compute majority vote across models for each sample.
Args:
predictions: Integer array of shape (n_samples, n_models)
Returns:
1D integer array of shape (n_samples,) with the most common class per row.
"""
# YOUR CODE HERE
pass