Hungarian Alignment of Cluster Labels
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `hungarian_align(y_true, y_pred)`. Build the class-by-cluster contingency table over sorted unique labels, then find the one-to-one cluster-to-class matching that maximizes the total count using `linear_sum_assignment`. Return a dict with keys `"mapping"`, `"accuracy"`, `"confusion"` and `"remapped"`:
- `mapping` is a `dict` from each matched cluster ID to its class label, both as Python `int`s.
- `accuracy` is a `float`: the matched count divided by `n`.
- `confusion` is an int array whose rows are the sorted classes. Column `j` is the cluster matched to class `j` (all zeros if class `j` has no match). Unmatched clusters are appended after those columns, in ascending ID order.
- `remapped` is an int array of `y_pred` mapped to classes, with unmatched clusters mapped to `-1`.
For example:
```python
y_true = [0, 0, 0, 1, 1, 1, 2, 2, 2]
y_pred = [2, 2, 1, 0, 0, 0, 1, 1, 1]
out = hungarian_align(y_true, y_pred)
# out["mapping"] == {2: 0, 0: 1, 1: 2}
# out["accuracy"] == 8/9
# out["confusion"] == [[2, 0, 1], [0, 3, 0], [0, 0, 3]]
# out["remapped"] == [0, 0, 2, 1, 1, 1, 2, 2, 2]
```
Cluster 2 holds two of the three class-0 points, and clusters 0 and 1 hold all of classes 1 and 2, so the best matching gets 8 of the 9 points right. The one class-0 point in cluster 1 shows up off the diagonal.
```hint Arbitrary label values
Labels such as 3, 5 and 7 cannot index an array directly. Turn each label set into dense indices first.
```
```hint Unequal counts
When the numbers of classes and clusters differ, the matching leaves some rows or columns unmatched. Decide how each of the four outputs treats them before you build `confusion`.
```
### Constraints & Assumptions
- Labels are integers, not necessarily contiguous. The number of classes and the number of clusters may differ.
- Raise `ValueError` if either input is not 1-D, the lengths differ, or the input is empty.
- `scipy.optimize.linear_sum_assignment` and NumPy are available.
### What a Strong Answer Covers
- Building the contingency table over sorted unique labels when IDs are arbitrary integers.
- Solving a maximum-weight one-to-one assignment, and why per-cluster majority voting is not a valid relabeling.
- Handling more classes than clusters, and more clusters than classes, in `mapping`, `confusion` and `remapped`.
- Input validation and the output types (Python ints in `mapping`, a float accuracy, int arrays).
- Time and space complexity of the table and the assignment.
### Follow-up Questions
- Why is the Hungarian algorithm needed instead of mapping each cluster to its majority class?
- How does this accuracy compare to ARI or NMI as an external metric?
- How would you scale this when there are thousands of clusters?
Overview: Write a function that maps predicted cluster IDs to ground-truth classes with a one-to-one optimal assignment on the contingency table, then reports accuracy, a reordered confusion matrix and remapped labels. It tests contingency tables, linear_sum_assignment and handling unequal numbers of classes and clusters.