Skip to content
E
Educatum

Precision/recall tradeoff

Curated by Educatum AI teamLast updated:
Model EvaluationMetrics
Direct Answer

The precision-recall tradeoff occurs because shifting a classifier's decision threshold moves false positives and false negatives in opposite directions. Raising the threshold makes positive predictions stricter—typically increasing precision while decreasing recall—whereas lowering the threshold captures more true positives (higher recall) at the expense of more false alarms (lower precision).

Study Card: Precision/Recall Tradeoff

Direct Answer

In binary classification with predicted probabilities or scores, moving the classification threshold tt changes which instances are predicted positive:

  • Increasing tt predicts fewer positives, lowering false positives (FP\text{FP}) while increasing false negatives (FN\text{FN}): recall decreases monotonically, and precision generally increases (though precision can occasionally dip at extreme thresholds if a high-scoring negative sits near the top).
  • Decreasing tt predicts more positives, capturing more true positives (TP\text{TP}) and increasing recall, while admitting more false positives and typically lowering precision.

Understanding this tradeoff allows practitioners to choose an operating threshold tailored to asymmetric business or clinical error costs rather than blindly defaulting to 0.5.

Key Terms

  • Precision: TPTP+FP\frac{\text{TP}}{\text{TP} + \text{FP}}, fraction of positive predictions that are correct.
  • Recall: TPTP+FN\frac{\text{TP}}{\text{TP} + \text{FN}}, fraction of actual positives retrieved.
  • False Positive (FP): Predicting positive when the true label is negative.
  • False Negative (FN): Predicting negative when the true label is positive.
  • Classification Threshold: Cutoff probability or score above which an instance is assigned to the positive class.

Example

Consider two asymmetric applications:

  • Spam Filtering: Blocking an important legitimate email (FP) is much more disruptive than letting an occasional spam email reach the inbox (FN). Setting a high threshold (e.g., 0.9) prioritizes high precision at the cost of lower recall.
  • Disease Screening: Missing a sick patient (FN) is far riskier than ordering a secondary diagnostic test (FP). Setting a lower threshold prioritizes high recall while tolerating lower precision.

Code Implementation

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_recall_curve
from sklearn.model_selection import train_test_split

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=2, n_informative=2, n_redundant=0, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train logistic regression
model = LogisticRegression()
model.fit(X_train, y_train)

# Predict probabilities for positive class
y_scores = model.predict_proba(X_test)[:, 1]

# Compute Precision-Recall curve across thresholds
precision, recall, thresholds = precision_recall_curve(y_test, y_scores)

plt.plot(recall, precision, marker='.')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve')
plt.grid(True)
plt.show()

# Evaluate precision and recall at a specific threshold
threshold_example = 0.6
predictions = (y_scores >= threshold_example).astype(int)

tp = np.sum((predictions == 1) & (y_test == 1))
fp = np.sum((predictions == 1) & (y_test == 0))
fn = np.sum((predictions == 0) & (y_test == 1))

precision_at_t = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall_at_t = tp / (tp + fn) if (tp + fn) > 0 else 0.0

print(f"Precision at threshold {threshold_example:.2f}: {precision_at_t:.2f}")
print(f"Recall at threshold {threshold_example:.2f}: {recall_at_t:.2f}")
  • F1 and FβF_\beta Scores: Single-number summaries combining precision and recall; FβF_\beta lets you weight recall β\beta times more heavily than precision when tuning thresholds.
  • Precision-Recall Curve vs. ROC Curve: ROC plots TPR against FPR across thresholds. On severely imbalanced datasets where true negatives (TN) vastly outnumber positives, FPR stays small even when FP is large—making ROC appear overly optimistic; Precision-Recall curves (and PR-AUC / Average Precision) are therefore generally more informative when evaluating rare positive classes.
  • Average Precision (AP): Summarizes the Precision-Recall curve as ∑n(Rn−Rn−1)Pn\sum_n (R_n - R_{n-1}) P_n, the recall-weighted mean precision across thresholds.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.