7/31
Part 2 of 6 · NDCGLearn and do
NDCG divides by the best order of everything you judged
Raw DCG has no fixed scale. A query with ten grade-3 results can reach a DCG@10 of 31.8, and a query with one can't pass 7. So an average of raw DCG over queries mostly measures which queries had many good results.
NDCG fixes that. It divides by the DCG of the ideal order, the best order possible:
IDCG@k is the DCG@k of the ideal order. Every query now scores between 0 and 1, and a perfect ranking scores 1.
Sort every item, then cut at k
def ndcg_at_k(rels: list[int], k: int) -> float:
"""DCG@k over the DCG@k of every item, sorted. 0 when nothing is relevant."""
# every item, sorted
ideal = dcg_at_k(sorted(rels, reverse=True), k)
return 0.0 if ideal == 0 else dcg_at_k(rels, k) / ideal
print(round(ndcg_at_k([3, 1, 2], 3), 4))
print(round(ndcg_at_k([1, 3], 1), 4))
The first line: NDCG@3 for [3, 1, 2], where the ideal is [3, 2, 1]. About what?
Pick one to see what it prints.
Pick an answer above to see the rest