-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_topn_ndcg.py
93 lines (63 loc) · 2.68 KB
/
test_topn_ndcg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import pandas as pd
from pytest import approx
from lenskit.metrics.topn import _dcg, ndcg
import lenskit.util.test as lktu
def test_dcg_empty():
"empty should be zero"
assert _dcg(np.array([])) == approx(0)
def test_dcg_zeros():
assert _dcg(np.zeros(10)) == approx(0)
def test_dcg_single():
"a single element should be scored at the right place"
assert _dcg(np.array([0.5])) == approx(0.5)
assert _dcg(np.array([0, 0.5])) == approx(0.5)
assert _dcg(np.array([0, 0, 0.5])) == approx(0.5 / np.log2(3))
assert _dcg(np.array([0, 0, 0.5, 0])) == approx(0.5 / np.log2(3))
def test_dcg_mult():
"multiple elements should score correctly"
assert _dcg(np.array([np.e, np.pi])) == approx(np.e + np.pi)
assert _dcg(np.array([np.e, 0, 0, np.pi])) == approx(np.e + np.pi / np.log2(4))
def test_dcg_empty2():
"empty should be zero"
assert _dcg(np.array([])) == approx(0)
def test_dcg_zeros2():
assert _dcg(np.zeros(10)) == approx(0)
def test_dcg_single2():
"a single element should be scored at the right place"
assert _dcg(np.array([0.5])) == approx(0.5)
assert _dcg(np.array([0, 0.5])) == approx(0.5)
assert _dcg(np.array([0, 0, 0.5])) == approx(0.5 / np.log2(3))
assert _dcg(np.array([0, 0, 0.5, 0])) == approx(0.5 / np.log2(3))
def test_dcg_nan():
"NANs should be 0"
assert _dcg(np.array([np.nan, 0.5])) == approx(0.5)
def test_dcg_series():
"The DCG function should work on a series"
assert _dcg(pd.Series([np.e, 0, 0, np.pi])) == \
approx((np.e + np.pi / np.log2(4)))
def test_dcg_mult2():
"multiple elements should score correctly"
assert _dcg(np.array([np.e, np.pi])) == approx(np.e + np.pi)
assert _dcg(np.array([np.e, 0, 0, np.pi])) == \
approx((np.e + np.pi / np.log2(4)))
def test_ndcg_empty():
recs = pd.DataFrame({'item': []})
truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]})
truth = truth.set_index('item')
assert ndcg(recs, truth) == approx(0.0)
def test_ndcg_no_match():
recs = pd.DataFrame({'item': [4]})
truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]})
truth = truth.set_index('item')
assert ndcg(recs, truth) == approx(0.0)
def test_ndcg_perfect():
recs = pd.DataFrame({'item': [2, 3, 1]})
truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]})
truth = truth.set_index('item')
assert ndcg(recs, truth) == approx(1.0)
def test_ndcg_wrong():
recs = pd.DataFrame({'item': [1, 2]})
truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]})
truth = truth.set_index('item')
assert ndcg(recs, truth) == approx(_dcg([3.0, 5.0] / _dcg([5.0, 4.0, 3.0])))