Mean reciprocal rank (MRR)Easy

Mean reciprocal rank (MRR)

Background

Mean Reciprocal Rank evaluates systems that return a ranked list where you mostly care about the first correct answer — question answering, entity retrieval, "I'm feeling lucky" search. For each query the reciprocal rank is 1/(position of the first relevant result)1/(\text{position of the first relevant result}); MRR averages it over all queries.

Problem statement

Implement mean_reciprocal_rank(rankings), where rankings[q] is a binary relevance list for query qq in ranked order (1 = relevant):

MRR=1Qq=1Q1rankq\text{MRR} = \frac{1}{Q}\sum_{q=1}^{Q}\frac{1}{\text{rank}_q}

where rankq\text{rank}_q is the 1-indexed position of the first relevant item in query qq's list (the term is 00 if no relevant item appears).

Input

  • rankingslist[list[int]]: each inner list holds 0/1 relevance flags in ranked order.

Output

Returns a float in [0,1][0, 1].

Examples

Example 1

Input:  rankings = [[0, 1, 0], [1, 0, 0], [0, 0, 1]]
Output: 0.6111

Explanation: the first relevant items sit at ranks 2, 1, 3, giving reciprocal ranks 1/21/2, 11, 1/31/3. Their mean is (0.5+1+0.333)/3=0.6111(0.5 + 1 + 0.333)/3 = 0.6111.

Constraints

  • Ranks are 1-indexed (the top result is rank 1).
  • A query with no relevant item contributes 00 to the sum.
  • MRR is the mean of the per-query reciprocal ranks; the result lies in [0,1][0, 1].

Notes

  • MRR rewards only the first hit — a perfect second result still caps a query's contribution at 1/21/2.
  • Contrast with NDCG/MAP, which credit all relevant results; MRR is the right choice when there is essentially one correct answer per query.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference example: 0.6111
  • All first-position hits -> 1.0
  • A query with no relevant item contributes 0
  • Reciprocal rank uses the FIRST relevant position only