Search and recommendation systems return a ranked list, and only the top few results are ever seen. Precision@k asks how much of the top k is relevant; Recall@k asks how much of everything relevant made it into the top k.
Implement precision_at_k(ranked, relevant, k) and recall_at_k(ranked, relevant, k).
ranked is a list of item ids, best first; relevant is a collection of the truly relevant ids.precision_at_k divides by k even if ranked is shorter than k; return 0.0 when k <= 0.recall_at_k divides by the number of relevant items; return 0.0 when there are none, or when k <= 0.Open one at a time. Each gives away a little more than the last.
Slice ranked[:k] and count how many of those ids are in the relevant set.
The two metrics share the hit count and differ only in the denominator.
No results yet
Run your code to check it against the examples, or submit to run every test.