- Accuracy hides the difference between false positives and false negatives.
- Precision, recall, and F1 score different kinds of mistakes.
- A threshold and the ROC curve show the trade-off; AUC summarizes ranking, not truth.
This lesson follows Overfitting, Bias–Variance & Regularization and leads into Naive Bayes & Gaussian Discriminant Analysis. The probability background is in Probability & Statistics.
1. The right question
A spam filter that labels every message “not spam” can be 95% accurate and still be useless: it never catches a single spam message. Accuracy answers “how often is the model right?” but not “which mistakes does it make?”
For a classifier, two errors have different costs. A false positive is a negative example labelled positive. A false negative is a positive example labelled negative. In a cancer screen, a false negative may be much worse than a false positive; in a fraud alert, the reverse may be true.
Before reading on, predict: if 9 of 10 examples are negative, what accuracy does the all-negative classifier get, and what is its recall?
2. The confusion matrix
A confusion matrix counts the four outcomes. Rows are the true labels; columns are the predictions.
TP is a positive example predicted positive. FP is a negative example predicted positive. FN is a positive example predicted negative. TN is a negative example predicted negative.
Accuracy is the fraction of all examples that are correct:
The matrix is more informative than the single number because it keeps the two error types separate. The next metrics turn those counts into rates.
3. Precision and recall
Precision asks: of the examples the model called positive, how many really were positive? It measures the cost of false alarms:
Recall asks: of the truly positive examples, how many did the model find? It measures missed positives:
The F1 score is the harmonic mean of the two. It is high only when both are high:
A model that always predicts positive has recall 1 and terrible precision. A model that predicts positive only when it is certain may have high precision and low recall. Neither number is “the score” without the task.
4. Thresholds
A probabilistic classifier outputs a score. The threshold turns it into a decision. Moving the threshold trades false positives against false negatives without changing the model’s ranking of examples.
Lower the threshold and more examples become positive: recall usually rises, precision usually falls. Raise it and the reverse happens. The best threshold depends on the cost of each error, not on accuracy alone.
Why can two teams disagree about which threshold is best for the same model?
5. ROC and AUC
The ROC curve plots the true positive rate against the false positive rate as the threshold sweeps from high to low:
The area under the curve (AUC) summarizes how well the scores rank positives above negatives. A perfect ranking has AUC 1; random ranking has AUC 0.5. Ties get half credit:
AUC is threshold-free, but it is not a complete decision rule. A model with high AUC can still be badly calibrated, and a model with lower AUC can be better at the operating point you actually need.
Does AUC 0.9 mean the model is 90% accurate?
6. Worked example
Six examples, with scores and true labels. At a threshold of 0.5, the predictions are shown in the last column.
| example | true label | score | prediction at 0.5 |
|---|---|---|---|
| 1 | 1 | 0.90 | 1 |
| 2 | 1 | 0.80 | 1 |
| 3 | 1 | 0.20 | 0 |
| 4 | 0 | 0.40 | 0 |
| 5 | 0 | 0.30 | 0 |
| 6 | 0 | 0.10 | 0 |
Worked example: two thresholds
| threshold | TP | FP | FN | TN | accuracy | precision | recall | F1 |
|---|---|---|---|---|---|---|---|---|
| 0.5 | 2 | 0 | 1 | 3 | 83.3% | 100.0% | 66.7% | 0.800 |
| 0.2 | 3 | 2 | 0 | 1 | 66.7% | 60.0% | 100.0% | 0.750 |
At 0.5, precision is perfect but one positive is missed. At 0.2, recall is perfect but two negatives become false positives. Accuracy falls from 83.3% to 66.7%; the matrix shows why.
The AUC for these six scores is 0.778. The ROC curve in the animation is the same ranking, viewed across every threshold.
Worked example: the accuracy trap
Nine negative examples and one positive example. A classifier that always says “negative” gets:
| TP | FP | FN | TN | accuracy | recall |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 9 | 90.0% | 0.0% |
90% accuracy and 0% recall. The high accuracy is real, but it describes a classifier that never finds the class it was built to find.
7. Where it fails
Class prevalence changes precision even when sensitivity and specificity stay fixed. A test that works in a hospital can look terrible in a low-prevalence screening population, or the reverse.
Aggregate metrics can hide subgroup failures. A model with good overall recall may miss one group almost completely. Report metrics by relevant subgroup and with uncertainty intervals, not just one decimal.
Reusing the test set to choose a threshold or a model turns it into a validation set. The final number is no longer an unbiased estimate of performance on new data. Leakage, duplicate rows, and future information create the same problem.
The next lesson asks where the probabilities come from in the first place. Generative classifiers build them from a model of each class.
Next on the recommended path: Naive Bayes & Gaussian Discriminant Analysis
8. Going deeper
Calibration asks whether predicted probabilities match observed frequencies. A model can rank well and still be overconfident. Reliability diagrams, Brier score, and log loss are different views of calibration and sharpness.
Precision-recall curves are often more informative than ROC when positives are rare. Average precision summarizes the area under that curve, but its exact definition varies between libraries and papers.
The cost matrix is the bridge from metrics to decisions. If a false negative costs ten times a false positive, the optimal threshold is generally lower than 0.5. The metric should reflect that asymmetry before the model is compared.
9. Practice
A model has TP = 8, FP = 2, FN = 4, TN = 86. What are precision, recall, and accuracy?
Why can a high-AUC model still be a bad alert system?
If you lower the threshold, which counts usually rise?
10. Resources
The MIT introduction defines loss, risk, and evaluation criteria. The scikit-learn guide covers confusion matrices, precision, recall, F1, ROC, and scoring APIs. Both links were opened and checked for this lesson.
Continue the track
The mathematics behind this topic: Probability & statistics.