metrics.py 949 字节
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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    def dcg(score_list):
        n = len(score_list)
        cost = .0
        for i in range(n):
            cost += float(score_list[i]) / np.log((i + 1) + 1)
        return cost

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


class NdcgTest(unittest.TestCase):
    def __init__(self):
        pass

    def runcase(self):
        a = [3, 2, 3, 0, 1, 2]
        value = ndcg(a)
        self.assertAlmostEqual(0.961, value, places=3)


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