Implement NMI, Homogeneity, Completeness and V-Measure From a Contingency Table
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given 1-D arrays `labels_true` and `labels_pred`, implement `mutual_information`, `normalized_mutual_info`, `homogeneity`, `completeness` and `v_measure(labels_true, labels_pred, beta=1.0)` from the contingency table. Each returns a Python `float` matching `sklearn.metrics` (`mutual_info_score`, `normalized_mutual_info_score`, `homogeneity_score`, `completeness_score`, `v_measure_score`) to `rtol=1e-7`.
Conventions:
- Use the natural log. NMI uses the arithmetic mean: `MI / (0.5 * (H(true) + H(pred)))`.
- Homogeneity is `MI / H(true)` and completeness is `MI / H(pred)`. Either score is `1.0` when its entropy is 0.
- V-measure is `(1 + beta) * h * c / (beta * h + c)`, or `0.0` when `h + c == 0`.
- NMI is `1.0` when both labelings have a single cluster, otherwise `0.0` when MI is 0.
- Mismatched shapes or non-1-D input raise `ValueError`.
Example:
```python
t = [0, 0, 1, 1]
p = [0, 0, 0, 1]
mutual_information(t, p) # ≈ 0.21576
homogeneity(t, p) # ≈ 0.31128
completeness(t, p) # ≈ 0.38369
v_measure(t, p) # ≈ 0.34372
normalized_mutual_info(t, p) # ≈ 0.34372
homogeneity([0, 0, 1, 1, 2], [0] * 5) # 0.0 (completeness 1.0, v 0.0, NMI 0.0)
```
```hint Shared quantities
All five scores are built from the same few numbers. Work out which ones, and compute them once from the label counts.
```
```hint Exact zeros
Several conventions branch on a value being exactly 0.0. Find the inputs where MI is 0 in exact arithmetic and check whether your floating-point code returns exactly 0.0 there.
```
### Constraints & Assumptions
- Labels are arbitrary hashable values (ints, negative ints, strings). Only the partition matters.
- Both arrays have equal length `n`, with `n` up to about 10⁴.
- All scores except MI lie in `[0, 1]`.
### Clarifying Questions to Ask
- What should the functions return for empty input: the sklearn values, or an error?
- Can either labeling have close to `n` distinct values, so that a dense `K_true × K_pred` table gets large?
- Do labels need to support values that cannot be sorted together, such as `None` mixed with integers?
### What a Strong Answer Covers
- A label encoding that depends only on the partition, and a contingency table built from it.
- MI and both entropies in natural log, with the MI sum taken over nonzero cells only.
- Every degenerate-case convention: zero entropies, `h + c == 0`, the single-cluster NMI rule and MI equal to 0.
- Floating-point care, so inputs whose MI is 0 in exact arithmetic return exactly 0.0.
- Input validation, and time and memory cost at the stated sizes.
### Follow-up Questions
- Why does NMI with arithmetic normalization equal V-measure at `beta=1`?
- Why prefer adjusted mutual information (AMI) for comparing many-cluster solutions?
- How would you scale this to millions of points with many clusters?
Overview: Implement mutual information, normalized mutual information, homogeneity, completeness and V-measure for two clusterings, matching scikit-learn's conventions. It tests entropy and contingency-table math, degenerate-case handling, and floating-point care around scores that should be exactly zero.