Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-223

NDCG at K

Problem

Compute NDCG at k: DCG = sum rel_i / log2(i + 2) for i < k, divided by the DCG of the ideal (descending) ranking.

Return 0.0 if the ideal DCG is 0 or k <= 0.

Hint

Discounts grow logarithmically with rank; the ideal ranking sets the ceiling.

Test cases

case 1
in: [[3,2,1],3]
expected: 1
case 2
in: [[0,2,1],3]
expected: 0.66967181649423
case 3
in: [[1,0,0],2]
expected: 1
case 4
in: [[0,0,0],3]
expected: 0
Starter codePython
import math
def ndcg_at_k(relevances, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor