TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Compute Mean Average Precision (mAP)

Metrics & Evaluation
Hard

For each retrieval query, rank items by descending score, compute Average Precision, then average the query scores to obtain mean Average Precision.

For one query:

AP⁡=1R∑r=1nPrecision⁡(r)rel⁡(r)\operatorname{AP} = \frac{1}{R}\sum_{r=1}^{n}\operatorname{Precision}(r)\operatorname{rel}(r)AP=R1​r=1∑n​Precision(r)rel(r)

Across QQQ queries:

mAP⁡=1Q∑q=1QAP⁡q\operatorname{mAP} = \frac{1}{Q}\sum_{q=1}^{Q}\operatorname{AP}_qmAP=Q1​q=1∑Q​APq​

Here, rrr is a rank, RRR is the total number of relevant items in that query, and rel⁡(r)\operatorname{rel}(r)rel(r) is one when the item at rank rrr is relevant. If k is provided, only ranks through k contribute to the numerator, while RRR remains the total relevant count. A query with no relevant items has AP zero. Return map_value and ap_per_query in a dictionary.

Loading visualization...

Examples

Input: y_true_list = [[1, 0, 1, 0]], y_score_list = [[0.9, 0.8, 0.7, 0.1]], k = None

Output: {"map_value": 0.833333, "ap_per_query": [0.833333]}

Explanation: Relevant items occur at ranks 1 and 3, where precision is 1 and 2/3.

Input: y_true_list = [[1, 0, 1], [1, 1, 0]], y_score_list = [[0.9, 0.8, 0.7], [0.9, 0.8, 0.7]], k = None

Output: {"map_value": 0.916667, "ap_per_query": [0.833333, 1.0]}

Hint 1

Use np.argsort(-scores, kind="stable") to obtain each ranking.

Hint 2

Use cumulative relevant counts divided by np.arange(1, n + 1) for precision at every rank.

Hint 3

Mask the precision array by the ranked relevance labels before summing.

Requirements

  • Sort each query by descending score
  • Compute precision only at relevant ranks, respecting the optional cutoff
  • Assign AP zero when a query has no relevant items
  • Return exactly map_value and ap_per_query in a dictionary

Constraints

  • Each relevance label is 0 or 1
  • Corresponding relevance and score lists have equal lengths
  • Use NumPy only
Try Similar Problems
Precision Recall At KHit Rate At KClassification MetricsIou Bounding BoxNdcg

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: any

You must run your code first.
PrevNext

Compute Mean Average Precision (mAP)

Metrics & Evaluation
Hard

For each retrieval query, rank items by descending score, compute Average Precision, then average the query scores to obtain mean Average Precision.

For one query:

AP⁡=1R∑r=1nPrecision⁡(r)rel⁡(r)\operatorname{AP} = \frac{1}{R}\sum_{r=1}^{n}\operatorname{Precision}(r)\operatorname{rel}(r)AP=R1​r=1∑n​Precision(r)rel(r)

Across QQQ queries:

mAP⁡=1Q∑q=1QAP⁡q\operatorname{mAP} = \frac{1}{Q}\sum_{q=1}^{Q}\operatorname{AP}_qmAP=Q1​q=1∑Q​APq​

Here, rrr is a rank, RRR is the total number of relevant items in that query, and rel⁡(r)\operatorname{rel}(r)rel(r) is one when the item at rank rrr is relevant. If k is provided, only ranks through k contribute to the numerator, while RRR remains the total relevant count. A query with no relevant items has AP zero. Return map_value and ap_per_query in a dictionary.

Loading visualization...

Examples

Input: y_true_list = [[1, 0, 1, 0]], y_score_list = [[0.9, 0.8, 0.7, 0.1]], k = None

Output: {"map_value": 0.833333, "ap_per_query": [0.833333]}

Explanation: Relevant items occur at ranks 1 and 3, where precision is 1 and 2/3.

Input: y_true_list = [[1, 0, 1], [1, 1, 0]], y_score_list = [[0.9, 0.8, 0.7], [0.9, 0.8, 0.7]], k = None

Output: {"map_value": 0.916667, "ap_per_query": [0.833333, 1.0]}

Hint 1

Use np.argsort(-scores, kind="stable") to obtain each ranking.

Hint 2

Use cumulative relevant counts divided by np.arange(1, n + 1) for precision at every rank.

Hint 3

Mask the precision array by the ranked relevance labels before summing.

Requirements

  • Sort each query by descending score
  • Compute precision only at relevant ranks, respecting the optional cutoff
  • Assign AP zero when a query has no relevant items
  • Return exactly map_value and ap_per_query in a dictionary

Constraints

  • Each relevance label is 0 or 1
  • Corresponding relevance and score lists have equal lengths
  • Use NumPy only
Try Similar Problems
Precision Recall At KHit Rate At KClassification MetricsIou Bounding BoxNdcg

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: any

You must run your code first.