In [1]:
import numpy as np
def log(base, x):
return np.log(x) / np.log(base)
In [2]:
direct_gain = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0]
count = len(direct_gain)
print(direct_gain)
[3, 2, 3, 0, 0, 1, 2, 2, 3, 0]
Cumulated Gain Vector: $$ CG_i = \left\{\begin{matrix} G_1 & i = 1\\ CG_{i-1} + G_i & i > 1 \end{matrix}\right. $$
In [3]:
CG = np.cumsum(direct_gain)
print(CG)
[ 3 5 8 8 8 9 11 13 16 16]
Discounted Cumulated Gain Vector: $$ DCG_i = \left\{\begin{matrix} CG_i & i <b\\ DCG_{i-1} + \frac{G_i}{\log_b(i)} & i \geq b \end{matrix}\right. $$
In [4]:
b = 3
DCGV = CG[:b-1]
for i in range(b-1, count):
DCGV = np.append(DCGV, DCGV[-1] + direct_gain[i] / log(2, i+1))
print(DCGV)
[3. 5. 6.89278926 6.89278926 6.89278926 7.27964207 7.99205644 8.65872311 9.60511774 9.60511774]
Best vector is the vector that is sorted in descending order.
In [5]:
best_vector = np.sort(direct_gain)[::-1]
print(best_vector)
[3 3 3 2 2 2 1 0 0 0]
In [6]:
best_CG = np.cumsum(best_vector)
print(best_CG)
[ 3 6 9 11 13 15 16 16 16 16]
In [7]:
IDCGV = best_CG[:b-1]
for i in range(b-1, count):
IDCGV = np.append(IDCGV, IDCGV[-1] + best_vector[i] / log(2, i+1))
print(IDCGV)
[ 3. 6. 7.89278926 8.89278926 9.75414238 10.52784799 10.88405518 10.88405518 10.88405518 10.88405518]
Normalized Discounted Cumulated Gain Vector (NDCG) is the ratio of DCG and DCG of the best vector.
In [8]:
NDCGV = DCGV / IDCGV
print(NDCGV)
[1. 0.83333333 0.87330208 0.77509868 0.70665252 0.69146535 0.73429033 0.79554201 0.8824944 0.8824944 ]