Averaging Ensemble Versus Best Single Regressor
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
You have precomputed predictions from several regression models (the ensemble members) on the same held-out samples. `preds` has shape (members, samples), so row `i` holds member `i`'s predictions. `weights` is optional and, when given, holds one non-negative weight per member.
Implement two functions:
- `average_predictions(preds, weights=None)` returns the mean prediction per sample, weighted when `weights` is given.
- `compare_to_best(preds, y, weights=None)` scores the ensemble and each member by mean squared error (MSE) on the held-out targets `y`.
Requirements:
- With `weights=None`, take the simple mean. Otherwise normalize the weights to sum to 1, so the result matches scikit-learn's `VotingRegressor`.
- Raise `ValueError` when `preds` is not 2-D or is empty, when the number of weights differs from the number of members, when any weight is negative or non-finite, when the weights sum to 0, or when `len(y)` differs from the number of samples.
- `compare_to_best` returns a dict with `member_mse` (array), `best_member` (int), `best_member_mse` and `ensemble_mse` (floats), and `beats_best` (bool).
- `best_member` is the index with the lowest MSE. On a tie, return the first such index.
- `beats_best` is `True` only when `ensemble_mse < best_member_mse` (strict).
Example:
```python
preds = [[1, -1, 2], [-1, 1, -2]]
y = [0, 0, 0]
compare_to_best(preds, y)
# member_mse=[2., 2.], best_member=0, best_member_mse=2.0,
# ensemble_mse=0.0, beats_best=True
average_predictions([[1, 2, 3], [3, 4, 5]], weights=[1, 3])
# -> [2.5, 3.5, 4.5]
```
In the first call the two members tie at MSE 2, so `best_member` is 0, and their average is 0 for every sample. In the second call the weights normalize to 0.25 and 0.75.
```hint Structure
Both functions need the same checks and the same normalized weights, so put them in one helper. With `preds` as a members-by-samples matrix, the ensemble prediction and the per-member errors are each one NumPy expression.
```
### Constraints & Assumptions
- `preds` is a 2-D array or nested list of numbers, members by samples. `weights` has one entry per member and `y` has one entry per sample.
- Member predictions are precomputed. Model fitting is out of scope.
- NumPy is available.
### What a Strong Answer Covers
- Validation that raises `ValueError` in every listed case, including members with zero samples and NaN or infinite weights.
- Weight normalization that reproduces `VotingRegressor`, with the uniform mean when `weights` is `None`.
- A vectorized ensemble prediction and per-member MSE, with the first-index tie rule for `best_member`.
- A strict comparison for `beats_best` and return values with the documented Python types.
- An explanation of how averaging can push error below the best member's.
### Follow-up Questions
- Why can a simple average beat the best member?
- How would you choose the weights without overfitting?
- Can a weighted average ever have a higher MSE than its worst member?
Overview: Implement a weighted averaging ensemble for regression in NumPy and compare its mean squared error with each member's on held-out targets. Tests input validation, weight normalization that matches VotingRegressor, tie handling, and an understanding of when an average of models beats its best member.