Precision-Recall Curve & Average Precision

medium
Unit 11 · Evaluation & Regression0/3 to milestone

For imbalanced problems (fraud, rare disease, retrieval), ROC AUC can look great while the model is useless at the top of the ranking. The precision-recall curve and average precision (AP) focus on the positives.

Sort predictions by score, highest first. Treat each distinct score as a threshold: everything at or above it counts as predicted positive. At each threshold, compute precision and recall. Then

AP=∑k(Rk−Rk−1) Pk\text{AP} = \sum_k (R_k - R_{k-1})\, P_k

over thresholds in order, with R0=0R_0 = 0. (This is scikit-learn's average_precision_score.)

Implement pr_curve(y_true, scores) returning (precisions, recalls, thresholds) in that order (highest threshold first), and average_precision(y_true, scores). Return nan if there are no positives.

#evaluation#ranking#classification2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Precision-Recall Curve & Average Precision · TechGradify