Problems
Loading...
1 / 1

Hit Rate at K

Recommender SystemsMetrics & Evaluation
Easy

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.

Formula

HR@K=1UuU1(top-Kurelevantu)\text{HR@K} = \frac{1}{|U|} \sum_{u \in U} \mathbb{1}(\text{top-K}_u \cap \text{relevant}_u \neq \emptyset)
Loading visualization...

Examples

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.

Hint 1

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.

Hint 2

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.

Requirements

  • For each user, check only the first K items in their recommendation list
  • A "hit" means at least one ground truth item appears in the top-K recommendations
  • Count the fraction of users who have at least one hit
  • Return 0.0 if there are no users

Constraints

  • recommendations and ground_truth have the same length
  • k >= 1
  • Return a float between 0.0 and 1.0
  • Time limit: 300 ms
Try Similar Problems