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

NDCG (Normalized Discounted Cumulative Gain)

Recommender SystemsMetrics & Evaluation
Medium

Normalized Discounted Cumulative Gain evaluates a ranked list while giving more weight to useful items near the top. For the first kkk items, compute:

DCG⁡@k=∑i=1k2ri−1log⁡2(i+1)\operatorname{DCG}@k=\sum_{i=1}^{k}\frac{2^{r_i}-1}{\log_2(i+1)}DCG@k=i=1∑k​log2​(i+1)2ri​−1​

Here, rir_iri​ is the relevance at one-based rank iii. Compute ideal DCG with the same formula after sorting all relevance scores in descending order:

NDCG⁡@k=DCG⁡@kIDCG⁡@k\operatorname{NDCG}@k=\frac{\operatorname{DCG}@k}{\operatorname{IDCG}@k}NDCG@k=IDCG@kDCG@k​

If kkk exceeds the list length, use every item. If ideal DCG is zero, return zero. Otherwise return NDCG as a Python float.

Loading visualization...

Examples

Input: relevance_scores = [0, 1, 2, 3], k = 4

Output: 0.547831

Explanation: The highest-relevance item appears last, so the discounted gain is well below the ideal ranking.

Input: relevance_scores = [3, 2, 1, 0], k = 4

Output: 1

Hint 1

Use enumerate(scores[:k], start=1) so each denominator uses the one-based rank.

Hint 2

Build ideal scores with sorted(relevance_scores, reverse=True).

Requirements

  • Compute DCG from the supplied ranking
  • Compute ideal DCG from descending relevance scores
  • Apply the cutoff to both rankings
  • Return zero when ideal DCG is zero

Constraints

  • Relevance scores are nonnegative numbers
  • The relevance list is nonempty
  • The cutoff is a positive integer
Try Similar Problems
Hit Rate At KPrecision Recall At KMean Average PrecisionTop K RecommendationsCatalog Coverage

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

NDCG (Normalized Discounted Cumulative Gain)

Recommender SystemsMetrics & Evaluation
Medium

Normalized Discounted Cumulative Gain evaluates a ranked list while giving more weight to useful items near the top. For the first kkk items, compute:

DCG⁡@k=∑i=1k2ri−1log⁡2(i+1)\operatorname{DCG}@k=\sum_{i=1}^{k}\frac{2^{r_i}-1}{\log_2(i+1)}DCG@k=i=1∑k​log2​(i+1)2ri​−1​

Here, rir_iri​ is the relevance at one-based rank iii. Compute ideal DCG with the same formula after sorting all relevance scores in descending order:

NDCG⁡@k=DCG⁡@kIDCG⁡@k\operatorname{NDCG}@k=\frac{\operatorname{DCG}@k}{\operatorname{IDCG}@k}NDCG@k=IDCG@kDCG@k​

If kkk exceeds the list length, use every item. If ideal DCG is zero, return zero. Otherwise return NDCG as a Python float.

Loading visualization...

Examples

Input: relevance_scores = [0, 1, 2, 3], k = 4

Output: 0.547831

Explanation: The highest-relevance item appears last, so the discounted gain is well below the ideal ranking.

Input: relevance_scores = [3, 2, 1, 0], k = 4

Output: 1

Hint 1

Use enumerate(scores[:k], start=1) so each denominator uses the one-based rank.

Hint 2

Build ideal scores with sorted(relevance_scores, reverse=True).

Requirements

  • Compute DCG from the supplied ranking
  • Compute ideal DCG from descending relevance scores
  • Apply the cutoff to both rankings
  • Return zero when ideal DCG is zero

Constraints

  • Relevance scores are nonnegative numbers
  • The relevance list is nonempty
  • The cutoff is a positive integer
Try Similar Problems
Hit Rate At KPrecision Recall At KMean Average PrecisionTop K RecommendationsCatalog Coverage

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.