The ranker showed grades [2, 3, 0, 1, 2, 3]. What is NDCG@5?
Pick one to see what it prints.
That's DCG@5, IDCG@5 and NDCG@5. Here they are row by row, your order on the left and the ideal on the right:
Rank
Shown: grade
Gain / disc
Ideal: grade
Gain / disc
1
2
3.00
3
7.00
2
3
4.42
3
4.42
3
0
0.00
2
1.50
4
1
0.43
2
1.29
5
2
1.16
1
0.39
6, below the cut
3
left out
0
left out
DCG@5 = 9.01
IDCG@5 = 14.60
NDCG@5 = 9.01 / 14.60 = 0.62.
Three things to read off it.
The grade-3 listing at rank 6 is the biggest miss
print(f"{ndcg_at_k(shown[:5], 5):.2f}")
Pick an answer above to see the rest
Predict
Build the ideal from the top 5 only, so the grade 3 at rank 6 never counts. What does NDCG@5 become?
Pick one to see what it prints.
Passing only the top 5 builds the ideal from those five alone:
Ideal built from
Its top 5
IDCG@5
NDCG@5
all six
3 3 2 2 1
14.60
0.62
the top 5 only
3 2 2 1 0
10.82
0.83: the grade 3 from rank 6 is gone
IDCG@5 drops to 10.82, and NDCG@5 reads 0.83 instead of 0.62. The miss vanishes from the score.
Exponential gain cares which good item comes first
print(f"{ndcg_at_k([3, 2, 0, 1, 2, 3], 5):.2f}")
Pick an answer above to see the rest
Predict
Swap ranks 1 and 2, so the grade 3 comes first: [3, 2, 0, 1, 2, 3]. What is NDCG@5?
Pick one to see what it prints.
That's the list with ranks 1 and 2 swapped, so the 3 is on top:
Rank 1
Rank 2
NDCG@5, exponential
NDCG@5, linear
as shown
2
3
0.62
0.71
swapped
3
2
0.72
0.77
Under exponential gain the 3 is worth 7 and the 2 is worth 3, so which one sits at rank 1 matters more. Under linear gain they're worth 3 and 2, and the same swap moves the score less.
In the editor, dcg_at_k and ndcg_at_k are done. Write best_swap(shown, k): try every swap of two ranks in shown, and return the two ranks, counted from 1, with the NDCG@k after the swap that scores highest.
Pick an answer above to see the rest
Instructions
1
Write best_swap(shown, k): try every swap of two ranks, and return the two ranks, counted from 1, and the NDCG@k after the swap that scores highest. On a tie, keep the first pair you tried.