Skip to content

Commit a249993

Browse files
authored
Merge pull request #215 from dzhwinter/fix_ndcg
metric ndcg with power 2
2 parents ad64207 + 857f4f6 commit a249993

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

ltr/metrics.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def dcg(score_list):
1919
n = len(score_list)
2020
cost = .0
2121
for i in range(n):
22-
cost += float(score_list[i]) / np.log((i + 1) + 1)
22+
cost += float(np.power(2, score_list[i])) / np.log((i + 1) + 1)
2323
return cost
2424

2525
dcg_cost = dcg(score_list)
@@ -28,14 +28,11 @@ def dcg(score_list):
2828
return dcg_cost / ideal_cost
2929

3030

31-
class NdcgTest(unittest.TestCase):
32-
def __init__(self):
33-
pass
34-
35-
def runcase(self):
31+
class TestNDCG(unittest.TestCase):
32+
def test_array(self):
3633
a = [3, 2, 3, 0, 1, 2]
3734
value = ndcg(a)
38-
self.assertAlmostEqual(0.961, value, places=3)
35+
self.assertAlmostEqual(0.9583, value, places=3)
3936

4037

4138
if __name__ == '__main__':

0 commit comments

Comments
 (0)