Given a ranked recommendation list, a collection of relevant items, and a cutoff k, compute precision at k and recall at k. Only the first k recommendations are evaluated.
Precision@k=k∣top-k∩relevant∣ Recall@k=∣relevant∣∣top-k∩relevant∣The numerator is the number of relevant items appearing among the first k recommendations. Return the two metrics as [precision, recall].
Input: recommended = [1, 3, 5, 7, 9], relevant = [1, 2, 3, 4, 5], k = 3
Output: [1.0, 0.6]
Explanation: All three top recommendations are relevant, giving 3/3 precision and 3/5 recall.
Input: recommended = [10, 20, 30], relevant = [1, 2, 3], k = 3
Output: [0.0, 0.0]
set(relevant) provides direct membership checks for the relevant items.
sum(item in relevant_set for item in recommended[:k]) counts the top-k hits.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Given a ranked recommendation list, a collection of relevant items, and a cutoff k, compute precision at k and recall at k. Only the first k recommendations are evaluated.
Precision@k=k∣top-k∩relevant∣ Recall@k=∣relevant∣∣top-k∩relevant∣The numerator is the number of relevant items appearing among the first k recommendations. Return the two metrics as [precision, recall].
Input: recommended = [1, 3, 5, 7, 9], relevant = [1, 2, 3, 4, 5], k = 3
Output: [1.0, 0.6]
Explanation: All three top recommendations are relevant, giving 3/3 precision and 3/5 recall.
Input: recommended = [10, 20, 30], relevant = [1, 2, 3], k = 3
Output: [0.0, 0.0]
set(relevant) provides direct membership checks for the relevant items.
sum(item in relevant_set for item in recommended[:k]) counts the top-k hits.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number