Precision, recall and the threshold: tune a classifier on your own scores
A classifier gives every case a score, but someone has to choose where to draw the line. Move the threshold and watch each case change sides, the confusion matrix fill up and precision and recall pull against each other.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
The chart places each of 22 cases by its model score along the bottom, at height 1 if it is truly positive and 0 if not. The orange step is the decision: everything at or right of the threshold is called positive. The dashed vertical line marks where the threshold is.
In the table, each case shows its score and true label, then the orange columns: the prediction and which cell of the confusion matrix it falls into. The results panel adds the four totals, precision, recall, the false positive rate, the F-beta score and plain accuracy.
The algorithm
Turning scores into decisions takes one comparison per case: positive if the score is at least the threshold. Each case then lands in exactly one of four cells. A true positive is a positive case called positive; a false positive is a negative case called positive; a false negative is a positive case missed; a true negative is a negative case correctly left alone.
Precision asks how trustworthy a positive call is: TP divided by everything called positive. Recall asks how much of the positive class was found: TP divided by all true positives. The false positive rate is the share of negatives that raise an alarm, which is the x-axis of a ROC curve.
The two pull against each other. Lowering the threshold calls more cases positive, so recall rises, but more negatives slip in and precision usually falls. Raising it does the opposite. The F-beta score combines them into one number: with beta equal to 1 it is the harmonic mean of precision and recall (the F1 score); a beta above 1 weights recall more, a beta below 1 weights precision more.
A worked example makes the trade-off concrete. At a threshold of 0.5 the example data gives 8 true positives and 4 false positives, so precision is 8 / 12 = 66.7%, and with 2 positives missed recall is 8 / 10 = 80%. Lower the threshold to 0.25 and the model finds all 10 positives, but it now raises 8 false alarms, so precision falls to 10 / 18 = 55.6%. Neither setting is right in general: the right one depends on what a missed positive costs compared with a false alarm.
Things to try
- Drag the threshold from 0.9 down to 0.1 and watch recall climb while precision drops.
- Find the threshold with the best F1 score, then set beta to 2 (missing a positive costs more, as in disease screening) and find the new best: it moves left.
- Set beta to 0.5 (a false alarm costs more, as in spam filtering) and see the best threshold move right.
- Paste your own model's scores and labels from a validation set, find your threshold, then download the results as CSV.
- Add a few rows of very confident mistakes (score 0.95, label 0) and see how much precision drops at high thresholds.
- Note how accuracy barely moves in places where precision and recall change a lot: with imbalanced classes accuracy hides the trade-off.
How it works in real systems
Most classifiers output a score, and the default threshold of 0.5 is rarely the right one. Teams pick the threshold on a validation set to match the cost of each kind of error, then check it on held-out data, because a threshold tuned on test data overstates performance. Precision-recall curves are preferred over ROC curves when positives are rare, since the false positive rate stays small even when most alarms are false.
The same reasoning governs LLM-based systems: a guardrail model that flags harmful prompts, a retrieval system that decides which documents are relevant, or an LLM judge that grades answers all end with a score and a threshold.