#324Mean Average Precision (MAP)MediumML System DesignEvaluation MetricsAsked atGoogle · Microsoft · Meta
Mean Average Precision (MAP)
Background
MAP (Mean Average Precision) averages AP across queries - a standard reranker / detection eval. Where AP measures precision over recall thresholds within one query, MAP just takes the mean of AP across many queries.
Problem statement
Implement mean_average_precision(retrieved_per_query, gold_per_query). Both inputs are lists with one entry per query: retrieved is a ranked ID list, gold is the set of relevant IDs.
Input
retrieved_per_query- list of ranked ID sequences.gold_per_query- list of iterables of relevant IDs (treated as sets).
Output
floatin . Queries with empty gold are skipped (no contribution to the average).
Examples
Input: retrieved=[["a","b","c"], ["x","y","z"]], gold=[{"a","c"}, {"y"}]
Output: AP(0) = (1/1 + 2/3) / 2 = 0.8333; AP(1) = 1/2; MAP = (0.8333 + 0.5)/2 ~ 0.6667
Constraints
- Lengths of the two lists must match. ValueError otherwise.
- Queries with empty
goldare skipped; if all queries have empty gold, return .
Notes
- vs MRR / NDCG. MRR cares only about the first hit; NDCG weights with positional discounts; MAP averages precision at every relevant-hit position.
Python
Loading...
▶ 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 two-query example ~ 0.6667
- •Perfect retrieval example gives 1
- •Sample single query with two golds