9 · Evaluation, Metrics & Diagnostics · Classification Metrics: Ranking & Probability

Log loss

Log loss is an evaluation quantity that compresses a particular aspect of predictive behaviour into a number. Its usefulness depends on whether that aspect matches the real decision cost, class prevalence and intended model output.

Reference lessonPython exampleVisual explanation
Intuition first

What this concept means in practice

Log loss is an evaluation quantity that compresses a particular aspect of predictive behaviour into a number. Its usefulness depends on whether that aspect matches the real decision cost, class prevalence and intended model output.

The practical value of Log loss comes from understanding both the transformation and the boundary around it: what information is allowed to enter, what assumption is being made, and how you know the result is still valid after the transformation.

A beginner-friendly way to reason about it is to start with a tiny case where the correct result can be checked independently. Once the mechanism is clear, scale the exact same reasoning to larger tables, pipelines or models.

PurposeUse as part of a metric set aligned to the task and decision.
MechanismCompute predictions on data not used to fit the model, derive the metric from labels/scores/probabilities as appropriate, and inspect uncertainty and relevant subgroups instead of reading one number in isolation.
EvidenceInspect intermediate and final output; compare with an independent expectation.
Main cautionOptimising a convenient metric can reward the wrong behaviour; always compare with a simple baseline and inspect errors.
Mechanism

Trace the operation from input to decision

Compute predictions on data not used to fit the model, derive the metric from labels/scores/probabilities as appropriate, and inspect uncertainty and relevant subgroups instead of reading one number in isolation.

1Input→
2Apply rule→
3Inspect state→
4Validate→
5Use result
Key rule
Metric choice is part of problem framing, not an afterthought.
Visual explanation

Make the structure visible

The interactive view uses a concept-specific plot when the topic maps naturally to one; otherwise it uses a workflow view instead of leaving a broken placeholder.

Loading visual…
Practical example

Where you would use it

In disease screening, missing a positive case may be more costly than a false alarm, so recall and the precision–recall trade-off may matter more than raw accuracy.

Use when
Use as part of a metric set aligned to the task and decision.
Pitfall

What can make the result misleading

Watch out
Optimising a convenient metric can reward the wrong behaviour; always compare with a simple baseline and inspect errors.

A useful diagnostic question is: Could the same code still run successfully if the analytical assumption were wrong? If yes, add an explicit validation check rather than relying on execution success.

Implementation

Miniature Python example

Keep the example small enough that you can inspect each stage manually.

Python
# Step 1 — Import only the named objects needed by the following steps, keeping dependencies explicit.
from sklearn.metrics import log_loss

# Step 2 — Compute the right-hand expression and store its result in `y_true` for the next step.
y_true = [0,0,1,1,1,0]
# Step 3 — Compute the right-hand expression and store its result in `prob` for the next step.
prob = [0.10,0.25,0.70,0.85,0.60,0.30]
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 1 · observations:", len(y_true))
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 2 · probability range:", min(prob), max(prob))
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 3 · log loss:", round(log_loss(y_true, prob), 3))
Expected / illustrative output
STEP 1 · observations: 6
STEP 2 · probability range: 0.1 0.85
STEP 3 · log loss: 0.297
Implementation checklist

Before you move on

  • Can you state what data or object enters the operation?
  • Can you explain what changes and what must remain invariant?
  • Have you checked the result on a tiny case you can verify independently?
  • Have you considered the main failure mode described above?
  • Can the operation be reproduced from code/formulas and documented assumptions?