Hit rate at K measures the fraction of users for whom at least one relevant item appears in their top-K recommendations. It is one of the simplest and most intuitive evaluation metrics for recommender systems. Unlike precision which cares about how many relevant items are in top-K, hit rate only asks: did we get at least one right?
Given recommendation lists (one per user), ground truth relevant items (one list per user), and a cutoff K, compute the hit rate.
Input:
recommendations = [[1,2,3],[4,5,6],[7,8,9]], ground_truth = [[1],[10],[7]], k = 3
Output:
0.6667
User 0: item 1 is in top-3 (hit). User 1: item 10 is not in top-3 (miss). User 2: item 7 is in top-3 (hit). Hit rate = 2/3.
Input:
recommendations = [[10,1,2,3],[10,4,5,6]], ground_truth = [[1],[4]], k = 1
Output:
0.0
Both relevant items exist in the recommendation lists but not in the top-1 position. K matters: only the first K items count.
For each user, slice the recommendation list to the first K elements. Convert both the top-K and ground truth to sets, then check if their intersection is non-empty.
Count the number of users with a hit and divide by the total number of users. Remember that a hit is binary per user: even if multiple ground truth items are in top-K, it counts as one hit.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Hit rate at K measures the fraction of users for whom at least one relevant item appears in their top-K recommendations. It is one of the simplest and most intuitive evaluation metrics for recommender systems. Unlike precision which cares about how many relevant items are in top-K, hit rate only asks: did we get at least one right?
Given recommendation lists (one per user), ground truth relevant items (one list per user), and a cutoff K, compute the hit rate.
Input:
recommendations = [[1,2,3],[4,5,6],[7,8,9]], ground_truth = [[1],[10],[7]], k = 3
Output:
0.6667
User 0: item 1 is in top-3 (hit). User 1: item 10 is not in top-3 (miss). User 2: item 7 is in top-3 (hit). Hit rate = 2/3.
Input:
recommendations = [[10,1,2,3],[10,4,5,6]], ground_truth = [[1],[4]], k = 1
Output:
0.0
Both relevant items exist in the recommendation lists but not in the top-1 position. K matters: only the first K items count.
For each user, slice the recommendation list to the first K elements. Convert both the top-K and ground truth to sets, then check if their intersection is non-empty.
Count the number of users with a hit and divide by the total number of users. Remember that a hit is binary per user: even if multiple ground truth items are in top-K, it counts as one hit.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number