#376Recall@k (Retrieval)Easy

Recall@k (Retrieval)

Background

For the retrieval stage of any funnel (RAG, recsys), the right metric is recall, not precision: did the candidate set contain the gold items at all? Precision is the ranker's job at the next stage. Recall@k is the fraction of gold items that appear in the top-k retrieved.

Problem statement

Implement recall_at_k(retrieved, gold, k) where retrieved is a ranked list of document IDs (most relevant first) and gold is the set of relevant IDs.

recall@k  =  retrieved[:k]goldgold\text{recall@}k \;=\; \frac{|\,\text{retrieved}[:k] \cap \text{gold}\,|}{|\,\text{gold}\,|}

Input

  • retrieved - sequence of IDs, ranked best-first.
  • gold - iterable of IDs (treated as a set).
  • k - int >= 1, cutoff.

Output

  • float in [0,1][0, 1]. Returns 00 if gold is empty.

Examples

Input:  retrieved=["a","b","c","d","e"], gold={"a","d","z"}, k=3
Output: 1/3 = 0.333...

Explanation: top-3 contains {a, b, c}; intersect with gold {a,d,z} = {a}; recall = 1/2 ... wait |gold|=3. Actually: top-3 ∩ gold = {a}, |gold|=3, so recall = 1/3.

Constraints

  • k >= 1. Raise ValueError otherwise.
  • If k > len(retrieved), use the full list (no padding).
  • Return Python float.

Notes

  • vs precision@k. Precision@k = hit count / k. Recall@k = hit count / |gold|. Retrieval optimises recall; ranking optimises precision (and ordering).
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 recall computation
  • Sample full recall
  • Example partial single hit