Implement fault-tolerant weighted soft voting that skips failing ensemble members
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `soft_vote_with_fallback(members, X, n_classes, weights=None, min_members=1, tol=1e-6)`. Call each callable member exactly once, in order, as `member(X)`. Skip failing members and return `(proba, used)`: the weighted average of surviving outputs and the list of their original indices.
The function must follow these rules:
- `weights=None` means equal weights. Raise `ValueError` before calling any member if the length is not one entry per member, or if any weight is non-finite or negative.
- Skip a member that raises any exception, or whose output (cast to a float array) is not shape `(len(X), n_classes)`, contains a non-finite or negative value, or has a row sum deviating from 1 by more than `tol`.
- Fewer than `max(min_members, 1)` survivors raises `RuntimeError`. Survivors with a total weight of 0 raise `ValueError`.
- Renormalize the surviving weights to sum to 1, combine the outputs, then renormalize each output row to sum to exactly 1.
Example:
```python
def crashing_fn(X):
raise RuntimeError("model unavailable")
X = [[0.0], [1.0]] # any two samples
P0 = [[0.8, 0.2], [0.4, 0.6]]
P2 = [[0.2, 0.8], [0.0, 1.0]]
members = [lambda X: P0, crashing_fn, lambda X: P2]
soft_vote_with_fallback(members, X, n_classes=2, weights=[1, 2, 3])
# -> ([[0.35, 0.65], [0.10, 0.90]], [0, 2]) # weights become 1/4, 3/4
```
```hint Where a member can fail
A member can fail by raising, or by returning something that is not a probability matrix. Check that your guard also covers the step that turns its return value into an array.
```
```hint Keep weights paired with members
After a skip, the position of a survivor in your list of outputs no longer matches its position in `members`. Look up each survivor's weight by its original index.
```
### Clarifying Questions to Ask
- Does a surviving member with weight 0 count toward `min_members`?
- Should `BaseException` subclasses such as `KeyboardInterrupt` also be swallowed, or only `Exception`?
- Is the `used` list enough, or should the function also report why each member was skipped?
### What a Strong Answer Covers
- Weight validation that runs before any member is called and raises the error type the spec asks for.
- One call per member inside an exception guard that also covers the float cast, followed by every output check: shape, finiteness, sign and row sums within `tol`.
- Weights renormalized over the survivors by original index, then each combined row renormalized.
- The order and type of the two survivor errors: `RuntimeError` for too few survivors, `ValueError` for zero total weight.
- Complexity, and a short argument that the result is always a valid probability matrix.
### Follow-up Questions
- How would you add per-member timeouts so one slow model cannot stall the request?
- Should renormalized weights shift the ensemble's calibration when a strong member drops out?
- How would you surface member failures to operators without failing the request?
Overview: Write a weighted soft-voting combiner for an ensemble of probability models that skips members which crash or return malformed outputs. It tests defensive validation, weight renormalization over the surviving members, precise error semantics and keeping a prediction service up when one model fails.