Implement multiclass log-loss (categorical cross-entropy) from scratch in NumPy
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `multiclass_log_loss(y_true, proba)`, the categorical cross-entropy. `y_true` is an integer array of shape `(n,)` with labels in `0..K-1`, and `proba` has shape `(n, K)` with column `j` holding the predicted probability of class `j`. Return the Python `float` `-(1/n) * sum_i log(proba[i, y_true[i]])`, using the natural log.
Example:
```python
y_true = [0, 2, 1, 2]
proba = [[.7, .2, .1],
[.1, .3, .6],
[.2, .7, .1],
[.05, .15, .8]]
multiclass_log_loss(y_true, proba) == 0.36182976573941633
```
```hint Which entries matter
Write the loss in its one-hot form and look at which terms of the double sum over samples and classes can be nonzero.
```
### Constraints & Assumptions
- `y_true` has shape `(n,)` with integer labels in `0..K-1`; `proba` has shape `(n, K)`.
- Only each row's true-label column contributes; ignore the other columns.
- Rows are valid distributions (strictly positive, summing to 1), so no clipping is needed.
### What a Strong Answer Covers
- Explains why only the true-class probability of each row enters the loss.
- Gathers those probabilities with vectorized indexing, with no Python loop over rows.
- Uses the natural log, averages over `n`, and returns a Python float.
- States the time and space cost.
- Names the pitfalls: a zero probability for the true class and labels that are not integers.
### Follow-up Questions
- Why do real implementations clip `proba` before taking the log?
- How would you vectorize this without a Python loop?
- If the model gave you logits instead of probabilities, how would you compute the same loss without overflow?
Overview: Implement categorical cross-entropy (multiclass log-loss) in NumPy from integer labels and a matrix of predicted class probabilities. It tests why only the true-class probability of each row matters, vectorized indexing without a loop, and handling zero probabilities.