#376Recall@k (Retrieval)EasyLLMsML System DesignEvaluation MetricsAsked atGoogle · OpenAI · Cohere
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.
Input
retrieved- sequence of IDs, ranked best-first.gold- iterable of IDs (treated as a set).k-int >= 1, cutoff.
Output
floatin . Returns ifgoldis 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