metrics.py 928 字节
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 13 14 15

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

18 19 20 21
    def dcg(score_list):
        n = len(score_list)
        cost = .0
        for i in range(n):
D
dongzhihong 已提交
22
            cost += float(np.power(2, score_list[i])) / np.log((i + 1) + 1)
23 24 25 26 27 28 29 30
        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 已提交
31 32
class TestNDCG(unittest.TestCase):
    def test_array(self):
33 34
        a = [3, 2, 3, 0, 1, 2]
        value = ndcg(a)
D
dongzhihong 已提交
35
        self.assertAlmostEqual(0.9583, value, places=3)
36 37 38 39


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