Concepts / Precision and Recall

Precision and Recall

A precision-recall curve plots precision against recall for every possible threshold, and each point is one threshold's trade-off.

  • Programming

Why One Model Can Give Many Different Answers

Imagine a trained model that looks at each sample and produces a score. The score is not yet a decision. Someone must still decide where the cutoff sits: scores above the cutoff are called positive, and scores below it are called negative. That cutoff is called the threshold. The same trained model can therefore behave very differently depending on which threshold you pick.

Precision and recall are the two measurements that describe this behavior, and they usually pull against each other. Moving the threshold to catch more of the real positive samples can let in more wrong positives, and tightening the threshold can do the reverse. This article teaches you to read that trade-off directly from the numbers, one threshold at a time, rather than trusting a single default setting.

The source describes the extreme case that shows why a threshold cannot be chosen carelessly. If everything is classified as positive, recall is 100%, yet the model is useless. A single high recall number on its own says nothing about whether the model is any good.

From a Score to a Yes-or-No Decision

A classifier that outputs a score must still turn that score into a yes-or-no decision. The cutoff used for that decision is the threshold. Changing the threshold changes the trade-off between precision and recall for the same trained model.

A threshold can also be set to meet a target. The source gives a concrete case: if a business needs to miss fewer than 10% of positive samples, it needs a recall of 90%. The difficulty is that a threshold which reaches that target may leave the model with poor precision. Hitting the target is only half of the job; the other half is checking what it costs in precision.

Because the right threshold is often unknown when a model is first built, practitioners want to see all possible trade-offs at once. The precision-recall curve is the tool for that. Instead of asking for one threshold, it shows the trade-off for every threshold the model's scores allow.

What a Precision-Recall Curve Plots

Definition

A precision-recall curve plots precision against recall for every possible threshold. Each point on the curve is one threshold's trade-off: the precision and recall that result from cutting the scores at that threshold.

In Python, the function that computes the curve is precision_recall_curve, found in sklearn.metrics. It needs two inputs: the ground-truth labels and the predicted uncertainties. The predicted uncertainties come from either the decision_function method or the predict_proba method of the model. The function returns three arrays: precision values, recall values, and thresholds.

python
Notes on the code
  • precision_recall_curve is the function from sklearn.metrics that builds the curve
  • The first input is the ground-truth labels
  • The second input is the predicted uncertainties, not the yes-or-no output of predict
  • The three returned arrays are precision values, recall values, and thresholds

The returned thresholds cover all possible thresholds, meaning all values that appear in the decision function, in sorted order. Those sorted values are what allow the curve to be drawn. This is why every point on the curve can be traced back to one specific threshold.

The source describes the curve as starting at the top-left corner for a very low threshold, where everything is classified as positive. Raising the threshold moves the curve toward higher precision and lower recall. The source also states that the more a model keeps recall high as precision goes up, the better it is.

Tracing the Threshold Sweep

Now trace a real sweep. Start at threshold 0.9 and lower the threshold step by step to 0.2. Each lower threshold makes the model label more samples as positive. Before reading any output, decide what you expect recall to do under that change, and whether precision should keep rising, falling, or do something else.

What do you think happens?

As the threshold drops from 0.9 to 0.2, does recall ever decrease?

  • Yes, recall falls at some step

  • No, recall never decreases in the printed sweep

Reveal answer
Answer:

No, recall never decreases in the printed sweep.

Recall stays the same or rises at every step: it is 0.333 at 0.9 and 0.8, rises to 0.667 at 0.7, reaches 1.000 at 0.6, and stays at 1.000 at 0.4 and 0.2. Lowering the threshold lets more samples be labeled positive, so recall can only hold or grow.

threshold 0.9precision 1.000, recall0.333threshold 0.8precision 0.500, recall0.333threshold 0.7precision 0.667, recall0.667threshold 0.6precision 0.750, recall1.000threshold 0.4precision 0.600, recall1.000threshold 0.2precision 0.500, recall1.000lower thresholdlower thresholdlower thresholdlower thresholdlower threshold
When the threshold is lowered one step at a time, recall never falls, precision moves up and down, and the trade-off shifts between 0.8 and 0.6.
ThresholdRecallPrecisionWhat moved
0.90.3331.000Starting point
0.80.3330.500Precision falls, recall unchanged
0.70.6670.667Recall rises, precision rises
0.61.0000.750Recall reaches 1.000, precision rises
0.41.0000.600Precision falls, recall unchanged
0.21.0000.500Precision falls, recall unchanged

The verified printed sweep, read one row at a time. Each row is one threshold and the precision and recall obtained there.

Read the table as evidence. Recall never decreases down the column. Precision does not follow a single direction: it starts at 1.000 at threshold 0.9, drops to 0.500 at 0.8, climbs to 0.667 at 0.7 and to 0.750 at 0.6, and then falls to 0.600 at 0.4 and to 0.500 at 0.2. Each row is one point on the curve, and each point is one threshold's trade-off.

Where the Expected Trend Breaks

A common expectation is that precision always falls as the threshold drops, because more samples get labeled positive and some of them must be wrong. The printed sweep contradicts that expectation in one specific place, and the printed output is the evidence for finding it.

precision at 0.8expected to keep fallingprecision at 0.80.500precision at 0.6expected lower than 0.500precision at 0.60.750threshold lowered
Between threshold 0.8 and threshold 0.6, the expected steady decline in precision does not appear: precision rises from 0.500 to 0.750 while the threshold is lowered.

The place to look is the transition from the threshold 0.8 line to the threshold 0.7 line and onward to the threshold 0.6 line. Precision rises from 0.500 to 0.667 to 0.750 across those lines. The recall column is consistent with the curve idea, but the precision column is not monotonic, so a learner who expected a steady decline in precision finds the discrepancy exactly at those rows.

  • Common mistake

    Assuming precision must fall whenever the threshold falls

    What it looks like

    Precision goes from 0.500 at threshold 0.8 up to 0.750 at threshold 0.6 in the printed sweep.

    Why it fails

    Lowering the threshold adds more positive labels, but the new labels may include more correct positives, so precision can move either way.

    Do this instead

    Read each printed row and let the numbers decide the direction, instead of relying on a general rule.

  • Common mistake

    Treating one threshold's values as the whole story

    What it looks like

    Reading only the threshold 0.9 row shows precision 1.000, which hides the drop to 0.500 at threshold 0.8.

    Why it fails

    Each threshold is one point, and the curve is only understood by reading the whole sequence.

    Do this instead

    Scan all the rows and note where precision and recall move in different directions.

  • Common mistake

    Reading recall as a measure of quality on its own

    What it looks like

    Recall of 1.000 at threshold 0.2 looks perfect, yet the source's all-positive case has recall 100% and is useless.

    Why it fails

    Recall can be maximized by labeling everything positive, which ignores precision.

    Do this instead

    Always read recall together with the precision at the same threshold.

Reading the Shape of a Curve

The source describes a shape feature of its example curve that tells you where a change is cheap and where it is expensive. On the left side the curve is relatively flat, which means recall does not drop much while precision is increased up to about 0.5. For precision greater than 0.5, each gain in precision costs a lot of recall.

This is the kind of information the curve reveals that a single number cannot. It is why an operating point should be chosen by looking at the curve rather than by accepting a default threshold.

Choosing an Operating Point

An operating point is the threshold chosen to meet a business requirement, such as 90% recall, while still keeping reasonable precision. Fixing an operating point helps an organization make performance guarantees to customers or to other groups inside the organization. The threshold is then set to meet that target, and the remaining question is whether the model still has reasonable precision there.

Worked example

Selecting a threshold for a 90% recall requirement

A business requires recall of at least 0.9. Using only the printed sweep, which threshold gives the best precision while meeting that requirement?

  1. 1
    Find the rows that meet the target

    Recall must be at least 0.9. The rows at thresholds 0.6, 0.4, and 0.2 all show recall 1.000. Thresholds 0.9, 0.8, and 0.7 show recall 0.333, 0.333, and 0.667, so they fail the target.

  2. 2
    Compare precision among the qualifying rows

    Among thresholds 0.6, 0.4, and 0.2, precision is 0.750, 0.600, and 0.500 respectively.

  3. 3
    Choose the highest qualifying precision

    The threshold 0.6 gives the highest precision (0.750) while still reaching recall 1.000, so it is the operating point that meets the requirement with the best precision in this sweep.

Result

Threshold 0.6, with recall 1.000 and precision 0.750, is the best operating point among the printed rows for a 90% recall requirement.

Comparing Two Classifiers on Their Curves

Different classifiers can perform well in different parts of the curve, which means at different operating points. A support vector classifier (SVC) and a random forest trained on the same dataset can have different curve shapes. A random forest has predict_proba but no decision_function, so its curve is built from the probability of class 1, written rf.predict_proba(X_test)[:, 1]. The default threshold for predict_proba in binary classification is 0.5, so that threshold is the one marked on the random forest curve.

random forestF1 0.610extremes of the curveleads at very high recallor precisionSVCF1 0.656middle of the curveleads near precision 0.7stronger atstronger at
The random forest performs better at the extremes, for very high recall or very high precision requirements, while the SVM leads around precision 0.7. The F1 score favors the SVC but captures only one point on each curve.

The source makes the point with the F1 score. Its printed values are 0.610 for the random forest and 0.656 for the SVC, which would favor the SVC. But the F1 score captures only one point on the precision-recall curve, the one given by the default threshold. Comparing whole curves can show differences that the F1 score would miss, which is why the source says comparing whole curves gives more detail than a single summary number.

Comparing two curves by eye is a manual process. For an automatic comparison, the source describes summarizing the curve as the integral, or area under it, known as the average precision. This summary does not depend on a single threshold or operating point. It is computed with average_precision_score, and the decision_function or predict_proba output must be passed in, not the output of predict, because the calculation considers multiple thresholds.

python
Notes on the code
  • average_precision_score summarizes the area under the curve
  • scores must come from decision_function or predict_proba
  • Passing the output of predict would discard the multiple thresholds the calculation needs

Under this summary the random forest and the SVC perform similarly, with the random forest slightly ahead, which differs from the earlier F1 comparison. Because average precision is the area under a curve running from 0 to 1, its value always lies between 0 (worst) and 1 (best). The source also states that the average precision of a classifier that assigns decision function values at random equals the fraction of positive samples in the dataset.

Practice Reading a Sweep

MEDIUM

Using only the printed sweep table, identify the first threshold at which recall reaches 1.000, and state what precision is at that threshold. Then say whether the threshold 0.7 row meets a recall requirement of 0.9.

Hints
  • Scan the Recall column from the top and stop at the first 1.000.
  • Read the Precision value on the same row.
  • Compare the threshold 0.7 recall value of 0.667 with the 0.9 requirement.

Key Takeaways from the Curve

  1. 1
    A precision-recall curve plots precision against recall for every possible threshold, and each point is one threshold's trade-off.
  2. 2
    Lowering the threshold never reduced recall in the printed sweep, but precision moved in both directions, rising between thresholds 0.8 and 0.6.
  3. 3
    An operating point is the threshold chosen to meet a requirement, and the curve shows what that choice costs in precision.
  4. 4
    Whole curves reveal where each classifier is stronger, and average precision summarizes the area under the curve as one value between 0 and 1.

At a glance

  • A precision-recall curve plots precision against recall at every threshold, and each point is one threshold's trade-off.
  • In the printed sweep, recall never decreased as the threshold dropped, while precision rose between thresholds 0.8 and 0.6 against a simple expectation.
  • An operating point is chosen to meet a requirement such as 90% recall, then checked for reasonable precision.
  • Comparing whole curves and average precision can reveal differences that a single F1 score at one threshold would miss.

Related concepts

More concepts starting with P