Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #1712

Precision-Recall Curve

The precision-recall curve plots precision against recall across every possible decision threshold — a more informative alternative to ROC-AUC specifically when the positive class is rare.

Why Not Just Use ROC-AUC on Imbalanced Data?

ROC-AUC's False Positive Rate formula, \(FP/(FP+TN)\), has a very large denominator when negatives vastly outnumber positives — so even a fairly large number of false positives barely moves FPR, and ROC-AUC can look deceptively strong. Precision, \(TP/(TP+FP)\), is directly sensitive to the absolute number of false positives relative to true positives — no large "true negative" denominator to dilute it — making the precision-recall curve far more revealing when positives are rare.

Graphical Intuition

Recall Precision a random/no-skill baseline sits much lower on imbalanced data

Precision typically starts high (strict threshold, few but confident positive predictions) and falls as recall increases (looser threshold catches more true positives, but with more false alarms too).

Python Implementation

from sklearn.metrics import precision_recall_curve, average_precision_score, PrecisionRecallDisplay
import matplotlib.pyplot as plt

y_true = [0]*90 + [1]*10   # a realistically imbalanced example
y_scores = [0.1]*85 + [0.6]*5 + [0.3]*3 + [0.7]*7   # illustrative scores

precision, recall, thresholds = precision_recall_curve(y_true, y_scores)
ap_score = average_precision_score(y_true, y_scores)
print("Average Precision:", ap_score)

PrecisionRecallDisplay.from_predictions(y_true, y_scores)
plt.show()

Using the Curve to Choose a Threshold

import numpy as np

f1_scores = 2 * (precision * recall) / (precision + recall + 1e-10)
best_threshold_idx = np.argmax(f1_scores)
print("Best threshold:", thresholds[best_threshold_idx])
print("At that threshold -- precision:", precision[best_threshold_idx], "recall:", recall[best_threshold_idx])

This is a genuinely practical technique: instead of accepting the default 0.5 threshold, scan the precision-recall curve for the threshold that best matches your actual priorities — maximizing F1, or hitting a minimum required recall while maximizing precision at that level.

Average Precision — The PR Curve's Version of AUC

Average Precision (AP) summarizes the precision-recall curve into one number, analogous to ROC-AUC — but computed specifically to remain meaningful and undiluted on imbalanced data, which is exactly why it's the standard "area under the curve" choice paired with PR curves rather than plain AUC.

Practical Use Cases

  • Rare-event detection: fraud, disease screening, defect detection — anywhere positives are a small minority
  • Choosing a business-appropriate decision threshold, informed by the actual precision/recall tradeoff at each point

Common Mistakes

  • Defaulting to ROC-AUC out of habit on a rare-event problem, missing how misleadingly strong it can look.
  • Choosing a threshold using only training-set precision/recall — always evaluate the tradeoff on held-out validation/test data.

Interview Relevance

Q: "Why would you prefer the precision-recall curve over ROC-AUC for a rare disease detection model?" With very few actual positives, ROC-AUC's false positive rate is diluted by a huge number of true negatives in its denominator, making even a poor precision look acceptable on the ROC curve — the precision-recall curve directly exposes how many false positives accompany each level of recall, which is the tradeoff that actually matters for a rare-event problem.

Practice Question

Explain why, on a dataset with 99% negative and 1% positive examples, a model could show a strong ROC-AUC (e.g. 0.9) while still having poor precision at any reasonable recall level.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →