Module 11 · Machine Learning: How Models Learn · Lesson 37 of 51
Classification and how to judge it
Scores and thresholds, the confusion matrix, precision, recall, F1, ROC and AUC, and choosing a threshold from business costs.
In this lesson · about 45 minutes
- Build a confusion matrix and compute accuracy, precision, recall and F1
- Explain the ROC curve and AUC
- Choose a threshold using the cost of each kind of mistake
Try it · data lab
Churn model: choose the threshold
A model gives every member a churn score. Move the cut-off and see the confusion matrix, precision, recall and the ROC curve change.
A churn model gives each of 300 members a score from 0 to 1. Members scoring above the threshold get a “we miss you” offer. Orange = members who really churned; grey = members who stayed.
| Predicted churn | Predicted stay | |
|---|---|---|
| Really churned | TP 55 | FN 12 |
| Really stayed | FP 39 | TN 194 |
Accuracy
83%
Precision
58.5%
Recall
82.1%
F1
0.683
The confusion matrix
| Predicted churn | Predicted stay | |
|---|---|---|
| Really churned | True positive (TP) | False negative (FN): missed |
| Really stayed | False positive (FP): false alarm | True negative (TN) |
- Accuracy = (TP + TN) ÷ all. Misleading when one class is rare.
- Precision = TP ÷ (TP + FP): of those we flagged, how many were right?
- Recall (sensitivity) = TP ÷ (TP + FN): of real churners, how many did we catch?
- F1 = 2 × precision × recall ÷ (precision + recall): one number balancing both.
ROC and AUC
The ROC curve plots recall (true positive rate) against the false positive rate for every possible threshold. AUC, the area under it, is the chance that a random churner scores higher than a random stayer: 0.5 is coin-flipping, 1.0 is perfect, 0.75–0.85 is typical of useful business models. AUC judges the ranking; the threshold is a separate business decision.
Choosing a threshold with money
Say a retention offer costs $5 and saves a churner worth $60 half the time. A true positive earns 0.5 × 60 − 5 = $25; a false positive costs $5; a missed churner costs nothing extra. Pick the threshold that maximises 25 × TP − 5 × FP.
Exercise
Metrics from scratch
Given arrays y (1 = churned) and score, write metrics(threshold) that predicts 1 when score >= threshold and returns (precision, recall, f1), each rounded to 3 decimals. Then set best_t to the threshold from np.arange(0.1, 0.95, 0.05).round(2) that gives the highest profit 25 × TP − 5 × FP.
Code that runs before yours
import numpy as np rng = np.random.default_rng(4) y = (rng.random(300) < 0.26).astype(int) score = 1 / (1 + np.exp(-(np.where(y == 1, 0.8, -0.8) + rng.normal(size=300))))
Decision scenario
Which threshold for fraud?
You built a fraud model for card payments at the café chain.
Flagged payments are held for a manual check, which annoys honest customers. A missed fraud costs about $40; a false alarm costs about $2 in staff time and some goodwill. The operations lead asks you to "just use 0.5".
What do you recommend?
0 of 3 options explored. Try the others to see how each would play out.
Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 1
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.