Normalized Discounted Cumulative Gain evaluates a ranked list while giving more weight to useful items near the top. For the first k items, compute:
DCG@k=i=1∑klog2(i+1)2ri−1Here, ri is the relevance at one-based rank i. Compute ideal DCG with the same formula after sorting all relevance scores in descending order:
NDCG@k=IDCG@kDCG@kIf k exceeds the list length, use every item. If ideal DCG is zero, return zero. Otherwise return NDCG as a Python float.
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
Use enumerate(scores[:k], start=1) so each denominator uses the one-based rank.
Build ideal scores with sorted(relevance_scores, reverse=True).
Sign in to take notes on this problem
Accepts: array
Accepts: number
Normalized Discounted Cumulative Gain evaluates a ranked list while giving more weight to useful items near the top. For the first k items, compute:
DCG@k=i=1∑klog2(i+1)2ri−1Here, ri is the relevance at one-based rank i. Compute ideal DCG with the same formula after sorting all relevance scores in descending order:
NDCG@k=IDCG@kDCG@kIf k exceeds the list length, use every item. If ideal DCG is zero, return zero. Otherwise return NDCG as a Python float.
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
Use enumerate(scores[:k], start=1) so each denominator uses the one-based rank.
Build ideal scores with sorted(relevance_scores, reverse=True).
Sign in to take notes on this problem
Accepts: array
Accepts: number