Average Precision in Different Contexts

1. Binary Classification

For anomaly detection or imbalanced classification:

from sklearn.metrics import average_precision_score
 
# y_true: actual labels (0 or 1)
# y_scores: predicted probabilities or scores
ap = average_precision_score(y_true, y_scores)

2. Object Detection

In object detection, AP is calculated per class and then averaged:

Steps:

  1. For each detected bounding box, calculate IoU (Intersection over Union) with ground truth
  2. If IoU > threshold (usually 0.5), count as True Positive, else False Positive
  3. Calculate precision-recall curve
  4. Compute AP for each class
  5. Mean Average Precision (mAP): Average AP across all classes

mAP Variants:

  • mAP@0.5: IoU threshold = 0.5
  • mAP@0.75: IoU threshold = 0.75
  • mAP@[0.5:0.95]: Average over multiple IoU thresholds (COCO metric)

3. Information Retrieval

In search engines and recommendation systems:

  • Precision@k: Precision of top-k results
  • Average Precision: Averages precision values at positions where relevant documents appear