Implement a Multiclass SVM Meta-Estimator With One-vs-Rest and One-vs-One Strategies
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a class `MulticlassSVM(base_factory, strategy)` that turns binary SVMs into a multiclass classifier. Each call to `base_factory()` returns a fresh binary estimator that exposes `fit(X, y)`, with `y` in {+1, -1}, and `decision_function(X)`, which returns one margin per row. The wrapper supports `strategy="ovr"` (one-vs-rest) and `strategy="ovo"` (one-vs-one). `fit` sets `classes_` to the sorted unique labels and returns `self`.
The wrapper must follow these rules:
- OvR: train one estimator per class on all rows, labeling that class +1 and every other class -1. `decision_function` returns shape `(n, K)`. `predict` is the argmax, with ties going to the smallest class.
- OvO: use the pairs `(i, j)` with `i < j` in lexicographic order of class index. Train each pair only on the rows of those two classes, with `classes_[i]` as +1. `decision_function` returns shape `(n, K(K-1)/2)`, one column per pair in that order.
- `ovo_votes(X)` returns `(votes, sums)`, both of shape `(n, K)`. A pairwise margin `f > 0` is a vote for class `i`; otherwise the vote goes to `j`. The margin sum adds `+f` to `i` and `-f` to `j`.
- OvO `predict` picks the class with the most votes. Ties go to the largest margin sum among the tied classes, then to the smallest label.
- Raise `ValueError` for an unknown strategy (in `fit`), for fewer than 2 classes, and when `ovo_votes` is called under OvR.
Example with `classes_ = [0, 1, 2]`, so the pairs are `(0, 1)`, `(0, 2)`, `(1, 2)`:
```text
pairwise f for sample 1: [1, 1, 1]
pairwise f for sample 2: [0, -2, 0.5]
votes = [[2, 1, 0], [0, 2, 1]]
sums = [[2.0, 0.0, -2.0], [-2.0, 0.5, 1.5]]
predict = [0, 1]
```
```hint Map columns back to classes
Store the `(i, j)` pairs in the same order you fit the estimators. Column `p` of the OvO decision matrix then tells you which two classes to credit.
```
```hint Check the zero margin
The vote test is strict. Trace the second example row by hand and see where a pairwise margin of exactly 0 sends its vote.
```
```hint Scope of the tie-break
Margin sums only compare classes that share the top vote count. Make sure a class with fewer votes can never win on margin alone.
```
### Constraints & Assumptions
- `X` is a 2-D array of shape `(n, d)` and `y` has length `n`.
- Labels can be any sortable values (integers or strings), not necessarily `0` to `K-1`.
- Each base estimator's `decision_function` returns `n` margins, as shape `(n,)` or `(n, 1)`.
### What a Strong Answer Covers
- How each binary subproblem is built: which rows it trains on, how labels map to +1 and -1, and why every subproblem gets its own `base_factory()` instance.
- The shape and column order of `decision_function` under each strategy.
- The vote and margin-sum bookkeeping in `ovo_votes`, including the boundary case of a zero margin.
- The full tie-breaking order for both strategies, and how predicted indices map back to the original labels.
- The three validation errors, and the training cost of OvR compared with OvO.
### Follow-up Questions
- When would you prefer OvO over OvR for kernel SVMs?
- Why are raw OvR margins not directly comparable across estimators, and what would you do about it?
- How would you parallelize `fit` while keeping the output deterministic?
Overview: Implement a multiclass wrapper around binary SVM estimators that supports one-vs-rest and one-vs-one strategies, with exact rules for decision shapes, pairwise voting and tie-breaking. It tests how you reduce a K-class problem to binary subproblems and handle class indexing, margins and input validation in NumPy.