Accuracy, Precision, Recall, ROC-AUC & Choosing a Metric
You'll learn to
- -Understand why "accuracy" alone can be a dangerously misleading metric
- -Correctly define precision and recall, and know when each one matters most
- -Compute precision, recall, and F1 from a confusion matrix, by hand and in code
- -Understand ROC curves and AUC as a threshold-independent way to evaluate a classifier
- -Know the standard fixes for class imbalance: weighting, oversampling, and undersampling
Accuracy, the percentage of predictions a model got right, is the most intuitive metric and also, frequently, the wrong one to optimize for. This chapter explains why, and introduces the metrics that usually matter more in practice.
The Accuracy Trap
Imagine a fraud detection model evaluated on data where only 1% of transactions are actually fraudulent. A model that just predicts "not fraud" for every single transaction, doing zero real work, achieves 99% accuracy and is completely useless. This is why accuracy alone is dangerously misleading whenever the classes are imbalanced, which describes an enormous share of real-world problems: fraud, rare diseases, safety incidents, and more.
The Confusion Matrix: Where These Metrics Come From
Every one of these metrics is built from four counts: true positives (TP, correctly flagged), false positives (FP, wrongly flagged), true negatives (TN, correctly left alone), and false negatives (FN, wrongly left alone). Once you have those four numbers, precision, recall, and everything else fall out as simple ratios.
Precision: Of What You Flagged, How Much Was Right?
Precision asks: of everything the model labeled positive, fraud, spam, disease present, what fraction actually was? High precision means few false alarms. When the model says "fraud," it usually means it.
Recall: Of What Was Actually True, How Much Did You Catch?
Recall asks the complementary question: of everything that actually was positive, what fraction did the model successfully catch? High recall means few misses. The model rarely lets a real case slip through undetected.
Precision and recall usually trade off against each other. Making a model more cautious, raising the threshold before it flags something, typically raises precision but lowers recall, and vice versa. There is rarely a free lunch. Choosing where to sit on that tradeoff is a product decision informed by which kind of mistake actually costs more.
F1 Score: One Number That Balances Both
When you need a single number instead of two, the F1 score combines precision and recall via their harmonic mean, which, unlike a simple average, stays low unless both precision and recall are reasonably high. A model with 100% precision and 1% recall gets an F1 near 2%, not 50%, correctly reflecting that it is still a bad model overall.
ROC Curves and AUC: Evaluating Across Every Threshold at Once
Precision, recall, and F1 all share a hidden dependency: they are computed at one specific decision threshold, recall the Probability chapter's question of what probability counts as spam enough to act on. Change the threshold and every one of these numbers changes too. The ROC curve sidesteps this by plotting classifier performance across every possible threshold at once, and AUC compresses that entire curve into a single number.
Drag the threshold yourself below and watch the point trace out the exact curve it belongs to.
Dragging the Threshold Along the ROC Curve
Every point on this curve is a different decision threshold. Drag the slider and watch the confusion matrix change underneath it.
threshold
0.50
TPR / FPR
0.80 / 0.20
TP
4
FP
1
FN
1
TN
4
The shaded area under the whole curve is the AUC: 0.800. It summarizes the model across every threshold on this slider at once, not just the one you're currently dragging to.
ROC-AUC has its own blind spot. Under severe class imbalance, the fraud-detection scenario from earlier in this chapter, the huge number of true negatives can make AUC look deceptively good even when precision is poor, since FPR = FP/(FP+TN) stays small simply because TN is enormous. In heavily imbalanced settings, a precision-recall curve is often the more honest picture than ROC-AUC.
Fixing Class Imbalance, Not Just Diagnosing It
The accuracy trap tells you a problem exists. Here is what to actually do about it, once you have confirmed your classes are imbalanced.
- -Class weighting multiplies each class's contribution to the loss by a weight, usually inversely proportional to how common that class is, so the rare class "counts more" during training and the model cannot get away with ignoring it.
- -Oversampling duplicates examples from the minority class, or synthesizes new ones. SMOTE (Synthetic Minority Oversampling Technique) is the standard synthesis approach. It creates new minority examples by interpolating between two real minority examples that are near each other, rather than just copying existing ones.
- -Undersampling randomly drops examples from the majority class instead, which is simpler but throws away real data, usually only sensible when the majority class has data to spare.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.