Sub topic 2
Precision, Recall, and the Cost of Errors
Assume you know basic Python, labeled classification data, train/test splits, and ratios. A binary classifier predicts either a positive class, such as fraud, or a negative class, such as legitimate.
Precision: how trustworthy are positive predictions
Precision measures how often a positive prediction is correct. It answers:
> Of all cases the model called positive, how many were truly positive?
The formula is:
\[ \text{precision} = \frac{TP}{TP + FP} \]
Here, TP is a true positive and FP is a false positive. A false positive happens when the model predicts positive but the true label is negative.
Suppose a fraud detector flags four transactions. Three are fraud and one is legitimate. Its precision is:
\[ \frac{3}{3+1}=0.75 \]
So 75% of its fraud alerts are correct.
| Prediction result | Meaning |
|---|---|
| True positive | Predicted positive, and positive was correct |
| False positive | Predicted positive, but the true class was negative |
High precision matters when investigating false alarms is expensive or disruptive.
Recall: how many real positives were found
Recall measures how many actual positive cases the model finds. It answers:
> Of all truly positive cases, how many did the model identify?
The formula is:
\[ \text{recall} = \frac{TP}{TP + FN} \]
FN is a false negative: a real positive that the model missed.
If there are four fraud transactions and the model finds three, recall is:
\[ \frac{3}{3+1}=0.75 \]
The model found 75% of the fraud. The remaining 25% became false negatives.
tp, fp, fn = 3, 1, 1
precision = tp / (tp + fp)
recall = tp / (tp + fn)
print(precision) # 0.75
print(recall) # 0.75Choosing a metric by the cost of errors
Choose the metric that reflects the more harmful error.
- Prefer precision when false positives cost more. For example, a system should avoid wrongly blocking legitimate payments.
- Prefer recall when false negatives cost more. For example, a medical screening system should miss as few serious cases as possible.
- Track both when both error types matter.
A model can improve recall by labeling more cases as positive, but this often creates more false positives and lowers precision. The right balance depends on the consequences, not on a universal target value.
Exercise
A model produces 8 true positives, 2 false positives, and 4 false negatives. What are its precision and recall?
Show answer
Precision = 8 / (8 + 2) = 0.80. Recall = 8 / (8 + 4) ≈ 0.67.