Implement a support ticket router with multi-class team, multi-label tag and ordinal urgency heads
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `TicketRouter(estimator_factory=None, tag_threshold=0.5)` with `fit(X, team, tags, urgency)` (returns `self`), `predict_proba(X)` and `predict(X)`. It treats team as multi-class, tags as a multi-label 0/1 matrix and urgency as ordinal. Both predict methods return a dict with keys `"team"`, `"tags"`, `"urgency"`.
Requirements:
- Team: one-vs-rest binary model per sorted class (`team_classes_`, `team_estimators_`). Proba is the positive scores normalised to sum to 1 (uniform if all scores are 0).
- Tags: one model per column in `tag_estimators_`. A constant column is not trained: store `None` and predict its constant. Predicted tag = proba `>= tag_threshold`.
- Urgency: for sorted `urgency_levels_`, train K-1 models on `y > level_k` (`urgency_estimators_`). Then $P(\text{level}_k) = P(y > \text{level}_{k-1}) - P(y > \text{level}_k)$, with outer terms 1 and 0, clipped at 0 and renormalised (uniform if all 0).
- `predict` takes the argmax for team and urgency, with ties going to the lowest sorted class or level. Tags output has shape `(n_samples, n_tags)` of ints.
- Raise `ValueError` if `tags` is not 2-D with `n_samples` rows, or there are fewer than 2 teams or urgency levels.
Example:
```python
# PositiveRate is an estimator whose predict_proba returns the training
# positive rate for every row.
team = ["x", "y", "y", "z", "z", "z"]
tags = [[1, 0], [1, 1], [0, 0], [0, 0], [1, 0], [0, 1]]
urgency = [0, 1, 1, 2, 2, 2]
router = TicketRouter(estimator_factory=PositiveRate).fit(X, team, tags, urgency)
proba = router.predict_proba(X)
proba["team"] # every row: [1/6, 1/3, 1/2]
proba["tags"] # every row: [0.5, 1/3]
proba["urgency"] # every row: [1/6, 1/3, 1/2]
router.predict(X) # every row: team "z", tags [1, 0], urgency 2
```
```hint One building block
Each head reduces to binary models created by the factory. A small helper that returns the positive-class column keeps the three heads short and consistent.
```
```hint Degenerate cases
Work out which binary targets can end up with a single class, which probability rows can sum to zero, and which index `np.argmax` returns on a tie.
```
### Constraints & Assumptions
- `X` has shape (n_samples, n_features). `team` and `urgency` have n_samples entries, and `tags` is an (n_samples, n_tags) 0/1 matrix.
- Urgency levels sort in their ordinal order.
- `estimator_factory` is a zero-arg callable returning a fresh object with `fit` and `predict_proba` (column 1 = positive). Default: `LogisticRegression(random_state=0)`.
- Use NumPy and scikit-learn.
### What a Strong Answer Covers
- A one-vs-rest team head with row normalisation and the uniform fallback.
- A binary relevance tag head that skips constant columns and applies the inclusive threshold.
- A Frank and Hall urgency head: cumulative targets, differencing with outer terms 1 and 0, clipping and renormalising.
- Input validation, tie handling, and the shapes and types of both predict outputs.
- Training cost in terms of the number of teams, tags and urgency levels.
### Follow-up Questions
- How would you exploit correlations between tags?
- Why can Frank and Hall produce negative level probabilities, and what are the alternatives?
- How would you choose per-tag thresholds instead of a single `tag_threshold`?
Overview: Implement a support ticket router that predicts a team with one-vs-rest models, tags with per-column binary models, and urgency with Frank and Hall cumulative thresholds. It tests combining classification decompositions, probability normalisation, constant-column handling and tie rules.