precision-recall curve
threshold
machine learning
performance metrics
data science

What is a threshold in a Precision-Recall curve?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A classifier usually outputs a score such as probability, not a final yes-or-no decision. A threshold is the cutoff used to convert that score into a positive or negative label. In a precision-recall curve, each point corresponds to a different threshold.

Threshold as Decision Boundary

For binary classification, a typical rule is:

  • Predict positive when score >= threshold.
  • Predict negative otherwise.

Changing this cutoff changes confusion matrix counts, which directly changes precision and recall.

Precision means how many predicted positives are truly positive. Recall means how many actual positives were found.

Numerical Example

python
1import numpy as np
2from sklearn.metrics import precision_score, recall_score
3
4y_true = np.array([1, 1, 0, 1, 0, 0, 1, 0])
5y_score = np.array([0.95, 0.72, 0.60, 0.40, 0.35, 0.20, 0.10, 0.05])
6
7for t in [0.8, 0.5, 0.3, 0.1]:
8    y_pred = (y_score >= t).astype(int)
9    p = precision_score(y_true, y_pred, zero_division=0)
10    r = recall_score(y_true, y_pred, zero_division=0)
11    print(f"threshold={t:.1f} precision={p:.3f} recall={r:.3f}")

As threshold decreases, recall typically increases because more instances are marked positive. Precision may decrease because false positives often rise.

How the PR Curve Is Built

A precision-recall curve evaluates many thresholds and plots precision against recall.

python
1import matplotlib.pyplot as plt
2from sklearn.metrics import precision_recall_curve
3
4precision, recall, thresholds = precision_recall_curve(y_true, y_score)
5
6plt.plot(recall, precision)
7plt.xlabel("Recall")
8plt.ylabel("Precision")
9plt.title("Precision-Recall Curve")
10plt.grid(True)
11plt.show()

The threshold array corresponds to most curve points. Endpoint handling can make array lengths differ by one element, which is expected.

Why One Threshold Is Not Universally Best

There is no globally correct threshold. The right value depends on cost tradeoffs.

Examples:

  • Fraud detection may favor higher recall to catch more fraud.
  • Content moderation may favor higher precision to reduce false accusations.
  • Clinical triage may accept lower precision for high-risk screening.

Threshold tuning is a decision policy, not just a model metric tweak.

Practical Threshold Selection Strategies

Common approaches include:

  • Maximize F1 score on validation data.
  • Set minimum recall target and choose highest precision that satisfies it.
  • Choose threshold that fits operational capacity, such as alerts per day.
python
1from sklearn.metrics import f1_score
2
3best_t, best_f1 = 0.5, -1
4for t in np.linspace(0.0, 1.0, 101):
5    y_pred = (y_score >= t).astype(int)
6    f1 = f1_score(y_true, y_pred, zero_division=0)
7    if f1 > best_f1:
8        best_f1, best_t = f1, t
9
10print(best_t, best_f1)

This provides a baseline, but final selection should include business constraints.

Class Imbalance and PR Curves

PR curves are particularly informative when positives are rare. ROC metrics can appear strong even while positive-class usefulness is poor.

If your use case focuses on retrieving rare positives, PR-based thresholding usually provides clearer practical guidance.

Use validation data with realistic class distribution. If production prevalence changes, threshold quality can degrade quickly.

Monitoring Threshold Performance in Production

Thresholding is not one-and-done. Monitor metrics at the chosen threshold over time.

Track:

  • Precision estimate from labeled outcomes.
  • Recall proxy when full labels are delayed.
  • Alert volume and reviewer workload.

Keep threshold configurable in deployment settings so updates do not require full model retraining for every policy adjustment.

Common Pitfalls

  • Treating default threshold 0.5 as always optimal.
  • Selecting threshold on training data and overestimating performance.
  • Optimizing only one metric without business-cost context.
  • Ignoring distribution shift after deployment.
  • Reading PR curves without linking points back to actual threshold values.

Summary

  • A threshold converts model scores into binary decisions.
  • Each threshold yields a specific precision and recall tradeoff.
  • Precision-recall curves visualize this tradeoff across cutoffs.
  • Threshold choice must align with business cost and capacity constraints.
  • Monitor and retune thresholds as data and operating conditions evolve.

Course illustration
Course illustration

All Rights Reserved.