metrics.py 922 字节
Newer Older
1 2 3 4 5
import numpy as np
import unittest


def ndcg(score_list):
D
dzhwinter 已提交
6 7 8 9 10
    """
    measure the ndcg score of order list
    https://en.wikipedia.org/wiki/Discounted_cumulative_gain
    parameter:
        score_list: np.array, shape=(sample_num,1)
D
dzhwinter 已提交
11 12

    e.g. predict rank score list :
C
caoying03 已提交
13
    >>> scores =  [3, 2, 3, 0, 1, 2]
D
dzhwinter 已提交
14
    >>> ndcg_score = ndcg(scores)
D
dzhwinter 已提交
15 16
    """

17 18 19 20
    def dcg(score_list):
        n = len(score_list)
        cost = .0
        for i in range(n):
D
dongzhihong 已提交
21
            cost += float(np.power(2, score_list[i])) / np.log((i + 1) + 1)
22 23 24 25 26 27 28 29
        return cost

    dcg_cost = dcg(score_list)
    score_ranking = sorted(score_list, reverse=True)
    ideal_cost = dcg(score_ranking)
    return dcg_cost / ideal_cost


D
dongzhihong 已提交
30 31
class TestNDCG(unittest.TestCase):
    def test_array(self):
32 33
        a = [3, 2, 3, 0, 1, 2]
        value = ndcg(a)
D
dongzhihong 已提交
34
        self.assertAlmostEqual(0.9583, value, places=3)
35 36 37 38


if __name__ == '__main__':
    unittest.main()