In [43]:
import numpy as np
In [55]:
y1 = [True, True, True, True, True]
y2 = [False, True, True, True, True]
y3 = [True, False, True, True, True]
y4 = [True, True, False, True, True]
y5 = [True, True, False, False, True]
y6 = [True, True, False, True, False]
In [67]:
def ap(rel):
k = len(rel)
return 1 / k * sum([
(1 / (i + 1)) * sum(rel[:i+1])
for i in range(k)
])
def ndcg(rel):
k = len(rel)
best = dcg([True] * k, k)
dcg_ = dcg(rel, k)
return dcg_ / best
def dcg(rel, k):
return sum([rel[r] / np.log(r + 1 + 1)
for r in range(k)])
def compare(y):
print('NDCG: {:.3f}, AP: {:.3f}'.format(ndcg(y), ap(y)))
In [68]:
compare(y1)
In [69]:
compare(y2)
In [70]:
compare(y3)
In [71]:
compare(y3)
In [73]:
compare(y2 + [True] * 100)
In [74]:
compare(y5 + [True] * 100)
In [75]:
compare(y1 + [False] * 100)
In [80]:
compare(y1 + [False] * 10000)