Precision recall and average precision
Quick answer
A precision-recall curve shows the precision and recall achieved at different score thresholds. Average precision (AP) summarizes precision weighted by increases in recall; it is not the same as trapezoidal area under the PR curve. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
AP versus trapezoidal area
from sklearn.metrics import average_precision_score,precision_recall_curve,aucy=[0,1];s=[.9,.1];p,r,_=precision_recall_curve(y,s)print(f"{average_precision_score(y,s):.3f} {auc(r,p):.3f}")Observed local output:
0.500 0.250The only positive is ranked last. Linear interpolation and recall-step weighting differ.
How it works
Sort scores descending and process all equal-score observations together. At each distinct threshold compute precision=TP/(TP+FP) and recall=TP/P. AP=sum((recall_j-recall_previous)*precision_j) in increasing-recall order. The trapezoidal PR area instead linearly interpolates precision between curve points, including the conventional endpoint recall=0, precision=1; it can differ substantially. With no positives, recall is undefined; this curriculum reports AP as null, even though library functions can return a numeric convention and warning. Positive class is 1. The prevalence P/n is the expected precision of an independent random selection and a useful no-skill reference, not a promise that finite-sample random AP equals prevalence. Precision depends on prevalence; compare PR curves on the same population. An operating threshold should be chosen on development data using costs and constraints, not by inspecting the final test PR curve. Report uncertainty via appropriate resampling and report when replicates have undefined metrics.
Group equal scores
from sklearn.metrics import average_precision_scorefor y in [[1,0,1],[0,1,1]]: print(f"{average_precision_score(y,[2,2,1]):.3f}")Observed local output:
0.5830.583Swapping labels within a tied score group does not change AP.
Common mistakes
Do not sort tied positives ahead of tied negatives, call every PR area AP, or compare AP across populations with very different prevalence without context.
Practice and next steps
For labels [1,0,1] and scores [3,2,1], compute AP as (1+2/3)/2. Compare the trapezoidal area and explain why they differ. Complete the knowledge check and staged exercise. Next: Ranking metric definitions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Precision recall and average precision — knowledge check
Apply precision recall and average precision to new scenarios.
How does AP weight precision?
How should equal scores be handled?
Is trapezoidal PR area always AP?
What is a useful no-skill precision reference?
Where should an operating threshold be selected?