Skip to content
DeepForgeby svx
All problems
HardNLPnlp-409

NDCG at K from Relevances

Problem

DCG at k is sum_{i=1}^{k} rel_i / log2(i + 1), and IDCG sorts the relevances descending before applying the same formula. Return DCG / IDCG, or 0.0 when IDCG is zero.

Given the relevance list and k, return nDCG at k.

Hint

The ideal DCG uses the best possible ordering of the same relevances.

Test cases

case 1
in: [[3,2,1],3]
expected: 1
case 2
in: [[0,0],2]
expected: 0
case 3
in: [[1,0,2],2]
expected: 0.38009376671593426
Starter codePython
def ndcg_at_k_from_relevances(relevances, k):
    # Your code here
    pass

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