Log Loss: The Chance Given to the Right Answer

Metrics & EvaluationEasy+10 XP+10

Log Loss: The Chance Given to the Right Answer

Accuracy only asks whether the model picked the right side. Log loss asks how confidently it did so — and punishes confident mistakes far harder than hesitant ones.

For one example with true label y (either 0 or 1) and predicted probability p that the label is 1:

loss=−(ylog⁡p+(1−y)log⁡(1−p))\text{loss} = -\big(y \log p + (1 - y) \log (1 - p)\big)loss=−(ylogp+(1−y)log(1−p))

Only one half of that ever survives: when y = 1 the second term vanishes and the loss is -log(p); when y = 0 the first vanishes and it's -log(1 - p). Either way it reduces to the negative log of the probability you assigned to the correct answer.

Task: write log_loss_per_sample(y_true, probs) returning a list — one loss per example, each rounded to 4 decimal places. Don't average them.

  • log is the natural logarithm (math.log).
  • Every probability is strictly between 0 and 1, so you never take the log of zero and never need to clip anything.
  • The loss is always positive. A perfect, confident answer approaches 0; p = 0.5 costs about 0.693 whichever way the label falls.

The per-sample list is more useful than the average while debugging: sort it and the worst few entries are the examples your model is confidently wrong about, which is usually where the mislabelled data lives.

0% confident100% confident

The curve is gentle on the right and shoots up on the left. Being almost certain about the wrong answer is punished without limit.

Examples

y_true = [1, 0, 1]
probs = [0.9, 0.2, 0.4]
returns [0.1054, 0.2231, 0.9163]
y_true = [0]
probs = [0.5]
returns [0.6931]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run