Lesson 4 of 5 · 2 min
Measuring retrieval
Recall at k asks how often a correct item appears in the first k results. Measure it before judging the answers built on top.
results = {"q1": ["d3", "d1", "d9"], "q2": ["d4", "d8", "d2"], "q3": ["d7", "d5", "d6"]}
correct = {"q1": "d1", "q2": "d2", "q3": "d0"}
for k in (1, 3):
hit = sum(correct[q] in results[q][:k] for q in results) / len(results)
print(f"recall at {k}: {hit:.2f}")
