All problems
nDCG at k
Problem
Compute normalized discounted cumulative gain at k. DCG sums gain(document) / log2(position + 1) over the first k retrieved positions using the gains in relevance, and IDCG uses the same formula on the gains sorted in descending order.
Return DCG / IDCG, or 0.0 when k is not positive or IDCG is 0. Missing documents have gain 0.
Hint
Discounts use log2(i + 2) for 0-based position i; build IDCG from all graded documents.
Test cases
case 1
in: [["a","b","c"],{"a":3,"b":2,"c":1},3]
expected: 1
case 2
in: [["b","a","c"],{"a":3,"b":2,"c":1},3]
expected: 0.9224945116765986
case 3
in: [["a"],{"a":3},0]
expected: 0
case 4
in: [["a"],{"a":0},1]
expected: 0
case 5
in: [["c","a"],{"a":2,"b":1,"c":3},2]
expected: 1
case 6
in: [[],{"a":5},3]
expected: 0
Starter codePython
import math
def ndcg_at_k(retrieved, relevance, k):
# Your code here
pass6 test cases · run your code against them in the editor