Implement voting margins and greedy margin-based ensemble pruning
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given a members × samples matrix `predictions` of class labels and true labels `y`, implement `ensemble_margins(predictions, y)` and `prune_by_margin(predictions, y, target_size)`. The margin of a sample is (votes for the true class minus max votes for any other class) / M, where M is the number of members being scored and the other-class term is 0 if no member votes for another class. Pruning repeatedly removes the member whose removal leaves the highest average margin, until `target_size` members remain.
Requirements:
- `ensemble_margins` returns a float array of length `n_samples`.
- `prune_by_margin` returns `(kept, removed, margin_history)`: `kept` sorted ascending, `removed` in removal order.
- `margin_history` is a NumPy float array: the full ensemble's mean margin, then the mean margin after each removal (length `M - target_size + 1`).
- Ties between candidate removals go to the lowest member index.
- Malformed shapes raise `ValueError`, and so does a `target_size` that is not an integer (bools included) or lies outside [1, M].
Example:
```python
P = [[0, 1, 0, 1],
[0, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 1, 0]]
y = [0, 1, 0, 1]
ensemble_margins(P, y) # [0.0, 0.0, 0.0, 0.5]
prune_by_margin(P, y, 2) # ([0, 1], [3, 2], array([0.125, 0.5, 0.75]))
```
```hint Count votes per label
Build a samples × labels table of vote counts first. The margin numerator for every sample comes straight out of it, including the case where only one label receives votes.
```
```hint Make ties deterministic
All candidate subsets in one round have the same size. Think about what you can compare instead of float averages so equal candidates always resolve the same way.
```
### Constraints & Assumptions
- `predictions` is 2-D with M ≥ 1 rows, and `y` is 1-D with one label per column.
- Labels are integers, possibly multiclass.
- `target_size` is an integer in [1, M].
### What a Strong Answer Covers
- A margin computation that handles multiclass votes, labels no member predicts, and the case where no member votes for another class.
- The greedy loop with the right divisor at each step, the history length, and the lowest-index tie rule.
- Validation that raises `ValueError` for bad shapes and for every bad `target_size`, including bools.
- A comparison that float rounding cannot break, such as integer numerator sums.
- Time and memory cost, and where the bottleneck is.
### Follow-up Questions
- How would you make each pruning step faster than recomputing margins for every candidate subset?
- Why might maximising average margin not maximise validation accuracy, and what alternatives exist?
- How would you pick `target_size` automatically without overfitting the validation set?
Overview: Implement per-sample voting margins for a classifier ensemble and a greedy pruning routine that drops the member whose removal leaves the highest average margin. It tests multiclass vote counting, deterministic tie-breaking, input validation and the cost of greedy subset search.