Exemplo n.º 1
0
def test_ndcg():
    ndcg = NDCG()

    assert ndcg.type == 'ranking'
    assert ndcg.name == 'NDCG@-1'

    assert 1 == ndcg.compute(np.asarray([1]), np.asarray([0]))

    ground_truth = np.asarray([1, 0, 1])  # [1, 3]
    rec_list = np.asarray([0, 2, 1])  # [1, 3, 2]
    assert 1 == ndcg.compute(ground_truth, rec_list)

    ndcg_2 = NDCG(k=2)
    assert ndcg_2.k == 2

    ground_truth = np.asarray([0, 0, 1])  # [3]
    rec_list = np.asarray([1, 2, 0])  # [2, 3, 1]
    assert 0.63 == float('{:.2f}'.format(ndcg_2.compute(
        ground_truth, rec_list)))
Exemplo n.º 2
0
    def test_ndcg(self):
        ndcg = NDCG()

        self.assertEqual(ndcg.type, "ranking")
        self.assertEqual(ndcg.name, "NDCG@-1")

        self.assertEqual(1, ndcg.compute(np.asarray([1]), np.asarray([0])))

        ground_truth = np.asarray([1, 0, 1])  # [1, 3]
        rec_list = np.asarray([0, 2, 1])  # [1, 3, 2]
        self.assertEqual(1, ndcg.compute(ground_truth, rec_list))

        ndcg_2 = NDCG(k=2)
        self.assertEqual(ndcg_2.k, 2)

        ground_truth = np.asarray([0, 0, 1])  # [3]
        rec_list = np.asarray([1, 2, 0])  # [2, 3, 1]
        self.assertEqual(
            0.63, float("{:.2f}".format(ndcg_2.compute(ground_truth,
                                                       rec_list))))