PeaBox Lab › PeaLang › Learn › Chapter 19 › Lesson 5

Part IV — Expert Production Engineering · Chapter 19 · Lesson 5 of 7 · 7 min read

Lesson 5: Multiclass Classification, Calibration, and Threshold Selection

Lesson level: Advanced

Purpose: Extend classification to more than two classes, make predicted probabilities trustworthy, and turn probabilities into decisions using explicit business costs.

Learning objectives

By the end of this lesson you will be able to:

5.1 Multiclass classification

Many targets have more than two values: a support ticket routed to billing, technical, or account teams; a risk band of low, medium, or high. Two common approaches extend logistic regression:

Prefer softmax when you need coherent probabilities across classes. If the classes are ordered (low < medium < high), consider whether a mistake of two bands is worse than a mistake of one, and reflect that in the evaluation.

5.2 Evaluating multiclass models

The confusion matrix becomes a k × k table: rows are actual classes, columns are predicted classes. Read it for systematic confusions — if many technical tickets are predicted as account tickets, the two classes may need better features or clearer labelling rules.

Precision, recall, and F1 are computed per class, treating each class as "positive" in turn. They are combined in two ways:

AverageHowEmphasizes
MacroUnweighted mean of per-class scoresEvery class equally; exposes poor performance on small classes
WeightedMean weighted by class frequencyOverall volume; can hide failure on rare classes
MicroComputed from pooled countsEquivalent to accuracy for single-label problems

Report the per-class table, the macro average, and the class counts. A weighted F1 of 0.88 with a macro F1 of 0.61 means at least one minority class is handled badly.

5.3 Calibration

A model is calibrated when its probabilities mean what they say: among all cases given a 30% probability, about 30% turn out positive. Ranking quality (AUC) and calibration are different properties. A model can rank perfectly yet be systematically overconfident, and probabilities are routinely miscalibrated after class weighting or resampling (Lesson 2).

Check calibration with a reliability plot: group predictions into bins (for example ten bins of predicted probability), then plot the mean predicted probability against the observed positive rate in each bin. Points on the diagonal indicate good calibration; points below it mean the model is overconfident in that range. Summarize with the expected calibration error: the bin-size-weighted average gap between predicted and observed rates. Always show bin counts, because sparse bins are noisy.

5.4 Recalibrating probabilities

If probabilities are miscalibrated, fit a small calibration model that maps raw scores to better probabilities, using data the main model was not trained on — a separate calibration split or cross-validated predictions:

Illustrative listing.

from pealang.ml import CalibratedClassifier
from pealang.ml.metrics import reliability_table, brier

cal = CalibratedClassifier(pipe, method="platt", cv=5, seed=42)
cal.fit(X_train, y_train)
p_raw = pipe.predict_proba(X_test)
p_cal = cal.predict_proba(X_test)
print(brier(y_test, p_raw), brier(y_test, p_cal))
print(reliability_table(y_test, p_cal, bins=10))

Calibration changes probabilities, not ranking (for monotonic methods), so AUC stays essentially the same while log loss and Brier score improve.

5.5 Choosing a threshold from costs

A threshold turns a probability into an action. The right threshold depends on what each error costs. If acting on a case costs cFP when it was unnecessary and failing to act costs cFN when action was needed, then for calibrated probabilities it is worth acting whenever the expected cost of not acting exceeds the cost of acting, which gives the threshold:

threshold = cFP / (cFP + cFN)

Worked example. In the churn case of Lesson 3, a retention call that turns out unnecessary costs 20, and a missed churner costs 200. The cost-based threshold is 20 / 220 ≈ 0.09 — far below 0.5. Comparing three operating points on the same 1,000-customer test set:

Operating pointTPFNFPExpected cost (20 × FP + 200 × FN)
High threshold4040208,400
Medium threshold (Lesson 3 example)5624646,080
Low threshold70101805,600

The lowest threshold flags almost three times as many customers yet costs least, because missed churners are ten times as expensive as unnecessary calls. In practice, compute expected cost over a fine grid of thresholds on validation data (not the test set), choose the minimum, and check it against operational limits such as call-centre capacity.

5.6 Capacity limits and review bands

Costs are not the only constraint. If the team can make only 150 calls a week, the operating rule becomes "call the 150 highest-probability customers", and the useful metric is precision among the top 150. Ranking quality (Lesson 3) matters most in this setting.

For high-stakes decisions, use two thresholds and an abstain band: automatically approve below the lower threshold, automatically act above the upper threshold, and send the uncertain middle to a human reviewer. Size the band by reviewer capacity, monitor how often it is used, and record reviewer outcomes as new labelled data.

For multiclass problems, the simplest rule chooses the most probable class, but costs can differ by confusion. Routing a security ticket to billing is worse than the reverse. A cost matrix with one cost per actual-predicted pair, applied to the calibrated probabilities, picks the class with the lowest expected cost.

Common mistakes

Guided practice

Hands-on lab: From scores to decisions

Review questions

Applied challenge

An insurer screens claims for possible fraud. Investigating a claim costs 150; an undetected fraudulent claim costs on average 4,000; investigators can handle 300 claims a month out of 20,000. Design the full decision policy: calibration method, threshold or top-k rule, abstain band, and the monthly report the investigation manager will receive. Show the expected cost of your policy compared with investigating at random.

Lesson summary

Multiclass problems need per-class evaluation and macro averages that expose weak classes. Probabilities must be calibrated before they drive cost-based decisions; check with reliability plots and fix with Platt or isotonic calibration on held-out data. Choose thresholds from explicit costs and operational capacity, use abstain bands for uncertain high-stakes cases, and document the decision policy as part of the model.

Transition to Lesson 6

A model is now a chain of preprocessing, estimator, calibration, and decision policy. Lesson 6 packages that chain into a single pipeline that can be validated, saved, reloaded, and reproduced exactly.

Lesson status: Ready for review.

← Previous Next →