Loading the problem
Before you start
Score a ranked list of recommendations with precision at k and recall at k.
A recommender shows a user only the top few items, so the metrics look only at
the top k. Let hits be the number of the first k recommended items that
are in the user's set of relevant items. Then
precision@k = hits / k
recall@k = hits / len(relevant)
Precision asks how much of what you showed was useful. Recall asks how much of what was useful you showed.
def precision_recall_at_k(recommended, relevant, k):
Input
recommended = [3, 1, 7, 5, 2]
relevant = {1, 2, 9}
precision_recall_at_k(recommended, relevant, k=3)
Output
(0.3333333333, 0.3333333333)
Reasoning
The top 3 items are 3, 1, 7. Only 1 is relevant, so hits = 1. Precision
is 1 / 3 and recall is 1 / 3, since there are 3 relevant items. Item 2 is
relevant but ranked fifth, so it does not count at k = 3.
recommended is a list of item ids in rank order with no repeats.recommended has fewer than k items, still divide precision by k.relevant is empty, recall is 0.0.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell