GMM Hard Labels With Entropy Uncertainty
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `hard_labels_with_uncertainty(resp, threshold=0.0)` for an `(n_samples, n_components)` GMM responsibility matrix. Return a tuple `(labels, entropy)`: each sample's hard label (the component with the highest responsibility, or `-1` if that maximum is below `threshold`) and its Shannon entropy in nats.
Example:
```python
resp = [[0.9, 0.1, 0.0],
[0.4, 0.35, 0.25],
[0.5, 0.5, 0.0]]
hard_labels_with_uncertainty(resp, threshold=0.45)
# labels -> [0, -1, 0]
# entropy -> [0.3251, 1.0805, 0.6931]
```
```hint Watch the zero entries
In NumPy, `0 * np.log(0)` is `nan`, while the entropy of a row with a zero entry is a finite number. Decide how that term should be computed.
```
### Constraints & Assumptions
- Rows are non-negative and sum to 1. A `resp` that is not 2-D raises `ValueError`.
- A maximum confidence equal to `threshold` keeps the label. Ties go to the lowest component index.
- Entropy uses the natural log, and zero entries contribute 0, so the result must be finite.
- `labels` is an integer array and `entropy` is a float array. Both have shape `(n_samples,)`. Entropy does not depend on `threshold`.
### What a Strong Answer Covers
- Hard labels from the row-wise argmax with the lowest-index tie rule, replaced by `-1` only when the maximum is strictly below the threshold.
- Per-row Shannon entropy in nats, with zero entries handled so the result is finite and raises no warnings.
- Validation of non-2-D input, and outputs with the required dtypes and shapes.
- A vectorized implementation, its cost, and a hand check of the example.
### Follow-up Questions
- How would you pick `threshold` in practice, and which values can never reject a sample?
- When do entropy and the margin between the top two responsibilities rank samples differently, and which would you use to choose samples for manual review?
- How would you make the entropy comparable across models with different numbers of components?
Overview: Convert a Gaussian mixture responsibility matrix into hard cluster labels with a confidence threshold and a per-sample Shannon entropy score in NumPy. It tests vectorized argmax with a tie rule, handling of zero probabilities in the entropy, and input validation.