Sub topic 2
From Scores to Predictions: Argmax and Thresholds
Assume you know binary logistic regression, train/test splits, and precision, recall, and F1. A model usually produces scores or probabilities first. A decision rule converts them into predicted labels.
Decision rules: argmax and thresholds
For a multiclass problem, exactly one class is correct. Softmax produces probabilities that compete and sum to 1. The argmax rule chooses the class with the largest probability.
For a multilabel problem, several labels can be correct at once. Each label has an independent sigmoid probability. A threshold, commonly 0.5, decides whether each label is present.
import numpy as np
logits = np.array([2.0, 1.0, -1.0])
exp = np.exp(logits - logits.max())
softmax = exp / exp.sum()
sigmoid = 1 / (1 + np.exp(-logits))
print("multiclass:", softmax.round(3), "class", softmax.argmax())
print("multilabel:", (sigmoid >= 0.5).astype(int))One-vs-rest and one-vs-one strategies
A multiclass model can be built from binary classifiers:
- One-vs-rest trains one classifier for each class. Each classifier separates its class from all other classes. With \(K\) classes, it trains \(K\) models.
- One-vs-one trains a classifier for every pair of classes. It trains \(K(K-1)/2\) models and combines their votes.
One-vs-rest is usually simpler and scales better. One-vs-one can help when pairwise boundaries are easier to learn, but prediction becomes more expensive as the number of classes grows.
Multilabel problem transformations
Multilabel targets can also be transformed into binary problems.
- Binary relevance trains one independent classifier per label. It is simple but ignores label relationships.
- Classifier chains pass earlier label predictions into later classifiers. This can model co-occurrence, but errors can propagate through the chain.
- Label powerset treats each unique label combination as one multiclass class. It captures combinations directly, but the number of possible classes can grow rapidly.
Evaluating multiclass predictions
A confusion matrix places true classes in rows and predicted classes in columns. Correct predictions appear on the diagonal. Off-diagonal cells show which classes the model confuses.
Inspect the matrix before relying on accuracy. Macro averages give every class equal weight, while weighted averages account for class frequency.
Exercise
A multilabel model returns probabilities [0.8, 0.4, 0.7]. What prediction does a threshold of 0.5 produce?
Show answer
The prediction is [1, 0, 1]. The first and third labels pass the threshold.