Precision-recall curves and thresholds
Choose an operating point when positives are rare: read the precision-recall curve, compare it with ROC, and pick a threshold from the cost of each error.
Lesson 38 of 6612 minIntermediate
You will learn to
- Read a precision-recall curve
- Choose a threshold from business costs
- Compare PR AUC and ROC AUC on imbalanced data
Before this lesson
Intuition
The ROC curve compares the share of positives caught with the share of negatives flagged. When positives are rare, the second share is measured against a huge group, and it can look small even when most alerts are wrong. The precision-recall curve avoids that. It asks two questions that matter directly to the person using the model: "Of the cases I flag, how many are real?" (precision) and "Of the real cases, how many do I find?" (recall).
Think of a fraud analyst with a limited number of hours. Every flagged transaction costs time to review. Precision tells her how much of that time is wasted. Recall tells her how much fraud still gets through. She cannot have both at their best. Sliding the threshold moves her along the curve: a lower threshold flags more transactions (higher recall, lower precision), and a higher one flags fewer (the reverse). Choosing a threshold is choosing a point on this curve, and the right point comes from costs, not from a rule such as "0.5".
Picture it
Each point on the curve is one threshold. The dashed line is the precision of a model with no skill: the share of positives in the data.
The math
For a threshold :
The area under the precision-recall curve, often computed as average precision, summarises it in one number. Unlike ROC, its baseline is not 0.5. A no-skill model has precision equal to the share of positives, , so the area of a no-skill model is about . On rare classes a PR area of 0.30 can be a big improvement over a baseline of 0.01.
To choose a threshold from costs, give each error a price. If a false negative costs and a false positive costs , the total cost at threshold is
and you choose the threshold that minimises it. If the model's probabilities are well calibrated (see the next lesson), the best rule is to flag a row when its probability is above .
Worked example
A fraud model scores 1,000 transactions, 50 of which are fraud. At four thresholds we count the outcomes (illustrative numbers):
| Threshold | TP | FP | FN | Precision | Recall | F1 |
|---|---|---|---|---|---|---|
| 0.3 | 45 | 120 | 5 | 45/165 = 0.273 | 0.90 | 90/215 = 0.419 |
| 0.5 | 40 | 40 | 10 | 40/80 = 0.500 | 0.80 | 80/130 = 0.615 |
| 0.7 | 30 | 10 | 20 | 30/40 = 0.750 | 0.60 | 60/90 = 0.667 |
| 0.9 | 15 | 1 | 35 | 15/16 = 0.938 | 0.30 | 30/66 = 0.455 |
The no-skill precision is 50 / 1,000 = 0.05, so even the 0.273 at threshold 0.3 is more than five times better than chance.
Now price the errors: a missed fraud costs 100 (money lost) and a false alarm costs 5 (review time).
- Threshold 0.3: 5 × 100 + 120 × 5 = 500 + 600 = 1,100
- Threshold 0.5: 10 × 100 + 40 × 5 = 1,000 + 200 = 1,200
- Threshold 0.7: 20 × 100 + 10 × 5 = 2,000 + 50 = 2,050
- Threshold 0.9: 35 × 100 + 1 × 5 = 3,500 + 5 = 3,505
The best F1 is at 0.7, but the lowest cost is at 0.3. That is not a contradiction: F1 assumes precision and recall matter equally, and here a miss costs twenty times more than a false alarm. The lesson is to decide the threshold from your own costs.
How mlflow does it
The Plots tab of a model candidate (binary classification only) has two plots for this lesson: Precision–Recall and Threshold sweep. The threshold sweep draws precision, recall, F1 and accuracy for 19 thresholds from 0.05 to 0.95, computed on the held-out test rows. Use it to read off the threshold that suits your costs. Multiclass runs have no threshold sweep. See Classification for the metrics and plots available per task.
Common mistakes
- Using 0.5 by habit. It is only optimal when both errors cost the same and probabilities are calibrated. Compute the cost at several thresholds.
- Choosing the threshold on the same rows you report. The best threshold on a sample is partly luck. Choose it on validation data and report on rows you did not use.
- Comparing PR areas across datasets. The baseline depends on the share of positives, so a 0.3 area means different things on different data. Compare with the no-skill line.
- Forgetting that precision depends on the class mix. If production has fewer positives than your test data, precision will be lower there. Estimate it with the production rate.
- Reading the curve as smooth. With few positives the curve is jagged, and one row can move it. Look at the counts behind each point.
Try it in mlflow
Open the threshold plot
- Open Runs, open a finished binary classification run and click a model candidate.
- Open the Plots tab and choose Threshold sweep. It shows precision, recall, F1 and accuracy for thresholds from 0.05 to 0.95.
- Find the threshold where F1 is highest, then the one where recall reaches the level your problem needs.
- Open the Precision–Recall plot and see how much precision you give up to reach that recall.