Implement a One-vs-One Multiclass Meta-Classifier with Margin Tie-Breaking
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a `OneVsOneClassifier` class that trains one binary model per unordered class pair. Its constructor takes `estimator_factory`, a zero-argument callable that returns a fresh estimator with `fit` and `decision_function` methods. The classifier predicts by majority vote over the pairwise models and breaks vote ties with cumulative pairwise margins.
The class must meet these requirements:
- `fit(X, y)` sets `classes_` (the sorted unique labels), `pairs_` (all `(i, j)` with `i < j`, in lexicographic order) and a parallel list `estimators_`. It raises `ValueError` when there are fewer than 2 classes and returns `self`.
- The model for pair `(i, j)` trains only on the rows of those two classes, in their original order, with label `1` for class `j` and `0` for class `i`.
- `votes(X)` returns an int array of shape `(n, K)`. A margin `s > 0` from pair `(i, j)` is a vote for `j`. Any other margin, including `s == 0`, is a vote for `i`.
- `decision_function(X)` returns `votes + conf / (3 * (abs(conf) + 1))`, where each pair adds its margin `s` to `conf[j]` and subtracts it from `conf[i]`. This matches the scoring formula scikit-learn uses for one-vs-one.
- `predict(X)` returns `classes_` at the argmax of the decision values, with exact ties going to the lowest class index. Labels come back in their original type, so string labels stay strings.
For example, with 3 classes and one row whose margins for pairs `(0, 1)`, `(0, 2)` and `(1, 2)` are `[1.0, -1.0, 0.5]`:
```text
votes -> [1, 1, 1]
conf -> [0.0, 0.5, -0.5]
decision_function -> [1.0, 1 + 1/9, 1 - 1/9]
predict -> classes_[1]
```
```hint Work per pair, not per row
Score every row with one pairwise model at a time, then update the two affected columns of the vote and confidence arrays with a boolean mask.
```
```hint Check the boundary cases
Trace a margin of exactly 0 through your vote update, and confirm which index your argmax returns when two decision values are equal.
```
### What a Strong Answer Covers
- Enumerating pairs in the required order and building each pair's training subset and binary labels correctly.
- Vote counting with the stated rule for zero margins, and confidence accumulation with the right signs.
- The bounded margin transform and how it interacts with integer vote counts.
- Mapping predictions back to the original labels, the lowest-index tie rule, and the fewer-than-two-classes error.
- Training and prediction cost as a function of the number of classes.
### Follow-up Questions
- Why does the margin term never override a strict vote majority?
- How does one-vs-one scale with many classes compared to one-vs-rest?
- How would you produce calibrated class probabilities from the pairwise models?
Overview: Implement a one-vs-one multiclass meta-classifier that trains a binary model for every class pair, predicts by majority vote, and breaks vote ties with summed pairwise margins. It tests careful pair bookkeeping, sign conventions, label handling, and reasoning about how a bounded confidence term interacts with vote counts.