Implement a Weighted Soft-Voting Classifier
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `SoftVotingClassifier(estimators, weights=None)` over a list of `(name, estimator)` pairs. `predict_proba` returns the weighted average of member probabilities, and `predict` returns the class label with the highest averaged probability.
The class must meet these requirements:
- `fit` fits a `clone` of each estimator (originals stay unfitted), stores them in `estimators_`, sets `classes_ = np.unique(y)` and returns `self`.
- `weights=None` means equal weights. Otherwise normalize the weights to sum to 1.
- `predict` maps the argmax to `classes_`. Ties pick the first (smallest) label.
- In `fit`, raise `ValueError` for an empty list, malformed pairs, non-string or duplicate names, or estimators lacking `fit`/`predict_proba`.
- Also raise `ValueError` for weights with the wrong length, non-finite or negative values, or a non-positive sum.
Results must match scikit-learn's `VotingClassifier(voting="soft")` with the same weights.
For example:
```text
classes_ = [5, 9]
weights = [3, 1] (normalized to [0.75, 0.25])
member a predict_proba: [[0.8, 0.2], [0.4, 0.6]]
member b predict_proba: [[0.2, 0.8], [0.0, 1.0]]
predict_proba(X): [[0.65, 0.35], [0.3, 0.7]]
predict(X): [5, 9]
```
Row 0 is `0.75 * [0.8, 0.2] + 0.25 * [0.2, 0.8] = [0.65, 0.35]`, so class 5 wins. Row 1 is `0.75 * [0.4, 0.6] + 0.25 * [0.0, 1.0] = [0.3, 0.7]`, so class 9 wins.
```hint Combining the members
Each member returns an `(n, k)` matrix. Stacking them gives a 3-D array, and the weights combine along one of its axes.
```
### Constraints & Assumptions
- Members are scikit-learn compatible, so `clone` works on them.
- Every member's `predict_proba(X)` returns an `(n, k)` array whose columns follow sorted class order.
### What a Strong Answer Covers
- Every validation the task lists for the estimator list and the weights, each raising `ValueError` before any member is fitted.
- Weight normalization, and why the blended rows stay valid probability distributions.
- Fitting clones so the caller's estimators stay unfitted.
- A vectorized weighted average over members, with the array shapes and the time and memory cost.
- Mapping the argmax back to `classes_`, the tie rule, and why the output matches scikit-learn's soft voting.
### Follow-up Questions
- How would you handle members trained on different class subsets?
- How would you choose the weights in practice?
- When can soft voting do worse than hard voting?
Overview: Implement a weighted soft-voting ensemble classifier in Python that averages member class probabilities with normalized weights and predicts the most probable label. It tests input validation, weight normalization, vectorized averaging in NumPy and agreement with scikit-learn's soft VotingClassifier.