Precision@k and recall@k are the standard metrics for evaluating top-k recommendation lists. Precision@k measures what fraction of the recommended items are relevant, while recall@k measures what fraction of all relevant items were recommended. Together they capture the trade-off between recommendation quality and coverage.
Given a ranked list of recommended items, a set of relevant (ground truth) items, and a cutoff k, compute both precision@k and recall@k.
Input:
recommended = [1, 3, 5, 7, 9], relevant = [1, 2, 3, 4, 5], k = 3
Output:
[1.0, 0.6]
Top-3 = [1, 3, 5]. All 3 are relevant. Precision = 3/3 = 1.0. Recall = 3/5 = 0.6.
Input:
recommended = [10, 20, 30], relevant = [1, 2, 3], k = 3
Output:
[0.0, 0.0]
None of the recommended items are relevant. Both precision and recall are 0.
Slice the recommended list to get the top k items: top_k = recommended[:k]. Convert the relevant items to a set for O(1) lookup. Count how many items in top_k appear in the relevant set.
Precision is hits/k, recall is hits/len(relevant). Return them as a two-element list [precision, recall].
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Precision@k and recall@k are the standard metrics for evaluating top-k recommendation lists. Precision@k measures what fraction of the recommended items are relevant, while recall@k measures what fraction of all relevant items were recommended. Together they capture the trade-off between recommendation quality and coverage.
Given a ranked list of recommended items, a set of relevant (ground truth) items, and a cutoff k, compute both precision@k and recall@k.
Input:
recommended = [1, 3, 5, 7, 9], relevant = [1, 2, 3, 4, 5], k = 3
Output:
[1.0, 0.6]
Top-3 = [1, 3, 5]. All 3 are relevant. Precision = 3/3 = 1.0. Recall = 3/5 = 0.6.
Input:
recommended = [10, 20, 30], relevant = [1, 2, 3], k = 3
Output:
[0.0, 0.0]
None of the recommended items are relevant. Both precision and recall are 0.
Slice the recommended list to get the top k items: top_k = recommended[:k]. Convert the relevant items to a set for O(1) lookup. Count how many items in top_k appear in the relevant set.
Precision is hits/k, recall is hits/len(relevant). Return them as a two-element list [precision, recall].
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number