TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Precision and Recall at K

Recommender SystemsMetrics & Evaluation
Easy

Given a ranked recommendation list, a collection of relevant items, and a cutoff kkk, compute precision at kkk and recall at kkk. Only the first kkk recommendations are evaluated.

Precision@k=∣top-k∩relevant∣k\mathrm{Precision@k} = \frac{\lvert \mathrm{top\text{-}k} \cap \mathrm{relevant} \rvert}{k}Precision@k=k∣top-k∩relevant∣​ Recall@k=∣top-k∩relevant∣∣relevant∣\mathrm{Recall@k} = \frac{\lvert \mathrm{top\text{-}k} \cap \mathrm{relevant} \rvert}{\lvert \mathrm{relevant} \rvert}Recall@k=∣relevant∣∣top-k∩relevant∣​

The numerator is the number of relevant items appearing among the first kkk recommendations. Return the two metrics as [precision, recall].

Loading visualization...

Examples

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]

Hint 1

set(relevant) provides direct membership checks for the relevant items.

Hint 2

sum(item in relevant_set for item in recommended[:k]) counts the top-k hits.

Requirements

  • Evaluate only the first k recommendations
  • Count each relevant recommendation once
  • Return [precision, recall] as a list of two floats

Constraints

  • recommended contains at least k unique item identifiers
  • relevant contains at least one unique item identifier
  • k≥1k \geq 1k≥1
Try Similar Problems
Hit Rate At KMean Average PrecisionNdcgTop K RecommendationsClassification Metrics

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Precision and Recall at K

Recommender SystemsMetrics & Evaluation
Easy

Given a ranked recommendation list, a collection of relevant items, and a cutoff kkk, compute precision at kkk and recall at kkk. Only the first kkk recommendations are evaluated.

Precision@k=∣top-k∩relevant∣k\mathrm{Precision@k} = \frac{\lvert \mathrm{top\text{-}k} \cap \mathrm{relevant} \rvert}{k}Precision@k=k∣top-k∩relevant∣​ Recall@k=∣top-k∩relevant∣∣relevant∣\mathrm{Recall@k} = \frac{\lvert \mathrm{top\text{-}k} \cap \mathrm{relevant} \rvert}{\lvert \mathrm{relevant} \rvert}Recall@k=∣relevant∣∣top-k∩relevant∣​

The numerator is the number of relevant items appearing among the first kkk recommendations. Return the two metrics as [precision, recall].

Loading visualization...

Examples

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]

Hint 1

set(relevant) provides direct membership checks for the relevant items.

Hint 2

sum(item in relevant_set for item in recommended[:k]) counts the top-k hits.

Requirements

  • Evaluate only the first k recommendations
  • Count each relevant recommendation once
  • Return [precision, recall] as a list of two floats

Constraints

  • recommended contains at least k unique item identifiers
  • relevant contains at least one unique item identifier
  • k≥1k \geq 1k≥1
Try Similar Problems
Hit Rate At KMean Average PrecisionNdcgTop K RecommendationsClassification Metrics

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.