Sub topic 6
From scores to predictions: argmax and thresholds
This chapter assumes you know binary logistic regression, how to read a predicted probability, and basic NumPy. A model outputs scores. A decision rule turns those scores into labels. The rule must match the target shape, or the predictions will not make sense.
Two rules for two shapes
A multiclass problem has exactly one correct class per sample. Softmax scores compete and sum to 1, so the natural rule is argmax: pick the class with the highest score.
A multilabel problem allows any subset of labels, including none. Each sigmoid score is an independent probability, so the natural rule is a threshold: predict label k when its probability is at least t.
Argmax
Argmax always returns exactly one class, even when the model is unsure. With probabilities of 0.34, 0.33 and 0.33, argmax still returns the first class. The returned class carries no sign of that uncertainty. If low-confidence cases matter, check the maximum probability separately and route those samples to review.
Argmax works on raw logits and on softmax outputs, because softmax preserves order. You only need softmax when you want probabilities.
Thresholds
The usual default is 0.5, but nothing makes it optimal. Lowering the threshold raises recall and lowers precision. Raising it does the opposite. Rare labels often need a threshold well below 0.5, because their probabilities rarely climb high.
A single global threshold is simple but treats every label the same. Per-label thresholds fit each label's own precision-recall trade-off. The chapter on threshold tuning covers how to choose them. Fit them on validation data, never on the test set.
The example below compares a global threshold with per-label thresholds, plus a fallback for empty predictions.
import numpy as np
proba = np.array([
[0.70, 0.40, 0.10],
[0.30, 0.20, 0.10],
[0.55, 0.60, 0.52],
])
# One global threshold for every label.
global_pred = (proba >= 0.5).astype(int)
# One threshold per label (broadcast across columns).
thresholds = np.array([0.5, 0.3, 0.15])
label_pred = (proba >= thresholds).astype(int)
# Fallback: if no label fires, switch on the highest-scoring one.
empty = label_pred.sum(axis=1) == 0
label_pred[empty, proba[empty].argmax(axis=1)] = 1
print("global:\n", global_pred)
print("per-label with fallback:\n", label_pred)Row 2 has no score above any threshold, so the global rule predicts no labels. The per-label rule also finds none, and the fallback assigns label 0. Row 1 shows the benefit of per-label thresholds: label 1 fires at 0.40 because its threshold is 0.3.
Common errors
- Applying softmax to a multilabel model. The scores then compete, so a sample with three true labels can never show high confidence in all three.
- Using argmax on multilabel output. You get one label per sample and silently lose the rest.
- Tuning thresholds on the test set. The reported metrics are then optimistic. Use a validation split or cross-validation.
- Ignoring empty predictions. Decide deliberately whether "no label" is a valid answer. If every sample must have a label, add the fallback.
- Trusting probabilities that are not calibrated. A threshold of 0.5 only means "50% likely" if the model is well calibrated. A later chapter covers calibration.
Exercise
A multilabel model outputs probabilities [0.45, 0.10, 0.30] for a sample. With a global threshold of 0.5, what is predicted? What does a fallback rule change?
Show answer
No label is predicted, because no probability reaches 0.5, so the output is [0, 0, 0]. A fallback that takes the top-scoring label would set label 0 (probability 0.45), giving [1, 0, 0].