Implement clipped multinomial log-loss in NumPy that matches sklearn's log_loss

Quick Overview

Implement the mean multinomial log-loss in NumPy from a probability matrix and integer labels, clipping probabilities before the log so the result matches sklearn's log_loss. It tests vectorized indexing, numerical safety for zero probabilities, and the softmax cross-entropy gradient.

Implement clipped multinomial log-loss in NumPy that matches sklearn's log_loss

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `multinomial_log_loss(P: np.ndarray, y: np.ndarray) -> float`. `P` has shape `(n_samples, n_classes)` with rows summing to 1, and `y` is a 1-D int array of labels in `0..n_classes-1`. Return the mean over samples of `-log(P[i, y[i]])`. Requirements: - Clip `P` into `[1e-15, 1 - 1e-15]` before the log, so a zero probability gives a large finite loss instead of `inf`. - Do not re-normalize `P` after clipping. - Return a Python `float`; a 0-d NumPy array does not count. - Match `sklearn.metrics.log_loss(y, P)` within `assert_allclose(rtol=1e-5)`. Example: ```python P = np.array([[0.7, 0.2, 0.1], [0.1, 0.8, 0.1], [0.2, 0.2, 0.6]]) y = np.array([0, 1, 2]) multinomial_log_loss(P, y) # -> 0.36354... (mean of -log(0.7), -log(0.8), -log(0.6)) ``` A zero probability on the true class is clipped to `1e-15`: ```python P = np.array([[0.0, 1.0, 0.0], [0.25, 0.25, 0.5]]) y = np.array([0, 2]) multinomial_log_loss(P, y) # -> about 17.616 (mean of -log(1e-15) and -log(0.5)) ``` ```hint Picking one entry per row Each sample contributes exactly one entry of `P`. Look for a way to select that entry for every row in a single indexing operation. ``` ### Constraints & Assumptions - `P` has shape `(n_samples, n_classes)` and each row sums to 1; `y` has shape `(n_samples,)` with integer labels in `0..n_classes-1`. - The grader uses a fixed 3-class input, so no shape validation or empty-array handling is needed. - Use the natural log. ### What a Strong Answer Covers - Gathers the true-class probability of each row with vectorized indexing. - Clips with the given bounds before the log and does not re-normalize. - Returns a Python float that agrees with `sklearn.metrics.log_loss` on the grader's input. - States the cost of the clip and of the gather. - Explains what the clip does to a zero probability and to the worst-case loss. ### Follow-up Questions - Why clip instead of adding a small epsilon inside `log`? - How would you also return the gradient with respect to the logits? - What would change if the rows of `P` did not sum to 1?

Overview: Implement the mean multinomial log-loss in NumPy from a probability matrix and integer labels, clipping probabilities before the log so the result matches sklearn's log_loss. It tests vectorized indexing, numerical safety for zero probabilities, and the softmax cross-entropy gradient.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement multinomial_log_loss(P: np.ndarray, y: np.ndarray) -> float. P has shape (n_samples, n_classes) with rows summing to 1, and y is a 1-D int array of labels in 0..n_classes-1. Return the mean over samples of -log(P[i, y[i]]).

Requirements:

  • Clip P into [1e-15, 1 - 1e-15] before the log, so a zero probability gives a large finite loss instead of inf .
  • Do not re-normalize P after clipping.
  • Return a Python float ; a 0-d NumPy array does not count.
  • Match sklearn.metrics.log_loss(y, P) within assert_allclose(rtol=1e-5) .

Example:

P = np.array([[0.7, 0.2, 0.1],
              [0.1, 0.8, 0.1],
              [0.2, 0.2, 0.6]])
y = np.array([0, 1, 2])
multinomial_log_loss(P, y)  # -> 0.36354...  (mean of -log(0.7), -log(0.8), -log(0.6))

A zero probability on the true class is clipped to 1e-15:

P = np.array([[0.0, 1.0, 0.0],
              [0.25, 0.25, 0.5]])
y = np.array([0, 2])
multinomial_log_loss(P, y)  # -> about 17.616  (mean of -log(1e-15) and -log(0.5))

Constraints & Assumptions

  • P has shape (n_samples, n_classes) and each row sums to 1; y has shape (n_samples,) with integer labels in 0..n_classes-1 .
  • The grader uses a fixed 3-class input, so no shape validation or empty-array handling is needed.
  • Use the natural log.

What a Strong Answer Covers Guidance

  • Gathers the true-class probability of each row with vectorized indexing.
  • Clips with the given bounds before the log and does not re-normalize.
  • Returns a Python float that agrees with sklearn.metrics.log_loss on the grader's input.
  • States the cost of the clip and of the gather.
  • Explains what the clip does to a zero probability and to the worst-case loss.

Follow-up Questions Guidance

  • Why clip instead of adding a small epsilon inside log ?
  • How would you also return the gradient with respect to the logits?
  • What would change if the rows of P did not sum to 1?