Skip to content

Add average precision#72

Merged
ablaom merged 11 commits intodevfrom
average-precision2
Mar 22, 2026
Merged

Add average precision#72
ablaom merged 11 commits intodevfrom
average-precision2

Conversation

@ablaom
Copy link
Copy Markdown
Member

@ablaom ablaom commented Mar 8, 2026

Replaces #70. From that PR:

Docstring
AveragePrecision()

Return a callable measure for computing the average precision. Aliases: average_precision.

AveragePrecision()(ŷ, y)

Evaluate AveragePrecision() on predictions ŷ, given ground truth observations y. It is expected that ŷ be a vector of distributions over the binary set of unique elements of y; specifically, ŷ should have eltype <:UnivariateFinite from the CategoricalDistributions.jl package.

Average precision is the area under the empirical precision-recall curve, understood as a step function. This is to be contrasted with measures going under the name "area under the precision-recall curve", in which the step function is usually replaced by a piece-wise linear approximation. Generally, differences between the two are only obvious when the number of observations is small, but it is faster to compute average precision.

Reference: Wikipedia entry, Average precision

Definition

Adopting each distinct predicted probability $p_1, p_2, \ldots, p_k$ for the positive class as a soft probability threshold for predicting an actual class, and assuming these thresholds are arranged in decreasing order, we obtain corresponding recalls $R_1, R_2, \ldots, R_k$ (monotonically increasing) and precisions $P_1, P_2, \ldots, P_k$. Adding an extra recall, $R_{k+1} = 1$, the average precision implemented here is defined as

$\sum_{j=1}^k P_j (R_{j+1} - R_j)$

In some other implementations, such as scikit-learn, $P_j$ is replaced by $P_{j+1}$. However, this requires the definition of a precision for unit recall, in the case the predicted positive class probabilities exclude 1.0, and this is avoided here.

Core implementation: Functions.average_precision.

Generally, an observation obs in MLUtils.eachobs(y) is expected to satisfy ScientificTypes.scitype(obs)<:``ScientificTypesBase.OrderedFactor{2}.

See also precision_recall_curve.

For a complete dictionary of available measures, keyed on constructor, run measures().

Traits

consumes_multiple_observations = true
can_report_unaggregated = false
kind_of_proxy = LearnAPI.Distribution()
observation_scitype = ScientificTypesBase.OrderedFactor{2}
can_consume_tables = false
supports_weights = false
supports_class_weights = false
orientation = StatisticalMeasuresBase.Score()
external_aggregation_mode = StatisticalMeasuresBase.Mean()
human_name = average precision

@ablaom ablaom mentioned this pull request Mar 8, 2026
1 task
@ablaom ablaom requested a review from OkonSamuel March 9, 2026 21:08
Make review suggestions.

Co-authored-by: Okon Samuel <39421418+OkonSamuel@users.noreply.github.com>
@ablaom
Copy link
Copy Markdown
Member Author

ablaom commented Mar 22, 2026

@OkonSamuel Thanks for your review. All suggestions have been adopted.

@ablaom ablaom merged commit 9db9cd2 into dev Mar 22, 2026
3 checks passed
@ablaom ablaom mentioned this pull request Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants