PR-AUC (Average Precision)
Background
On imbalanced data (fraud, click-through, retrieval), ROC-AUC is misleading because the false-positive rate is meaningless when negatives dominate. Average Precision (AP) = area under the PR curve is the canonical replacement: it averages precision over every recall threshold, focusing the metric on the minority class.
Problem statement
Implement pr_auc(y_true, scores) returning AP:
where the indices walk decreasing score order. , are precision and recall after seeing the top- scored items.
Input
y_true- array-like of labels, length .scores- array-like of floats, predicted scores. Higher = more likely positive.
Output
floatin . Returns if no positives iny_true.
Examples
Example 1 - perfect ranking
Input: y_true=[1,1,0,0], scores=[0.9, 0.8, 0.7, 0.6]
Output: 1.0
Example 2 - inverted ranking
Input: y_true=[0,0,1,1], scores=[0.9, 0.8, 0.7, 0.6]
Output: 0.4167...
Explanation: walking top-down, P=[0,0,1/3,1/2], R=[0,0,1/2,1]; AP = (1/2)(1/3) + (1/2)(1/2) = 0.4166. Add the boundary correctly.
Constraints
- Sort by score descending. Ties broken arbitrarily but deterministically.
- Empty input or zero positives -> return .
- Return a Python
float.
Notes
- Why AP, not ROC-AUC. ROC plots TPR vs FPR; at 0.1% prevalence, FPR is dominated by trivial negatives and the curve looks great regardless. AP only cares about precision at each recall - the minority class drives the score.
- Tie handling. Production implementations interpolate ties (sklearn's
average_precision_scoreuses a step-function variant). The simple per-item form above is the most common interview answer.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •reference inverted ranking
- •example interleaved labels
- •sample scattered positives