Neural Mastery
← Practice
Retrieval Recall@Kmedium

Retrieval Recall@K

medium RAG & Retrieval 15 min

NumPy allowed · plain Python earns 10 bonus points

RAG step 1/4

A query retrieves context

Embedding similarity ranks the documents before generation.
Open lesson →
01query
02embed
03retrieve
04context
What is RAG?→ embed →top context
retrieval.md44%
rag-systems.md33%
agents.md24%

Cosine-like similarity scores ranked before context assembly

Implement Recall@K for the retrieval stage specifically, evaluating whether the retriever found the known-relevant documents at all (a prerequisite for the generator to ever use them).

Task

Implement retrieval_recall_at_k(retrieved_ids, relevant_ids, k). Return |set(retrieved_ids[:k]) intersect relevant_ids| / len(relevant_ids). Return 0.0 if relevant_ids is empty.

Function Signature

retrieval_recall_at_k(retrieved_ids: list[str], relevant_ids: set, k: int) -> float

Examples

Example 1: full recall
Input: retrieved_ids = ['a', 'b', 'c'] relevant_ids = ['a', 'b'] k = 3
Output: 1
Example 2: partial recall
Input: retrieved_ids = ['a', 'x', 'y'] relevant_ids = ['a', 'b'] k = 3
Output: 0.5
Example 3: k truncates before relevant item found
Input: retrieved_ids = ['x', 'y', 'a'] relevant_ids = ['a'] k = 2
Output: 0

Constraints

  • k >= 1
Python3Saved ✓
def retrieval_recall_at_k(retrieved_ids, relevant_ids, k):
# Your implementation here
pass

Case 1: full recall
Input: retrieved_ids = ['a', 'b', 'c'] relevant_ids = ['a', 'b'] k = 3
Expected: 1
Case 2: partial recall
Input: retrieved_ids = ['a', 'x', 'y'] relevant_ids = ['a', 'b'] k = 3
Expected: 0.5
Case 3: k truncates before relevant item found
Input: retrieved_ids = ['x', 'y', 'a'] relevant_ids = ['a'] k = 2
Expected: 0