-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recommender.py
173 lines (148 loc) · 6.64 KB
/
test_recommender.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from compare_sets import jaccard_coefficient, similarity_matrix, similar_users, recommendations
from alternative_methods import asymmetric_similarity, minhash_similarity, minhashed
from read_file import read_file
from minhash import minhash
import unittest
from functools import reduce
__author__ = 'lene'
class TestRecommender(unittest.TestCase):
def test_jaccard(self):
self.assertEqual(jaccard_coefficient({'a', 'b'}, {'b', 'a'}), 1.)
self.assertEqual(jaccard_coefficient({'a', 'b'}, {'c', 'd'}), 0.)
self.assertAlmostEqual(jaccard_coefficient({'a', 'b'}, {'a', 'c'}), 1. / 3.)
def test_read_file(self):
csv = read_file('testdata.csv')
self.assertIsInstance(csv, dict)
self.assertEqual(len(csv), 5)
self.assertDictEqual(
csv,
{1: {12, 99, 32}, 2: {32, 77, 54, 66}, 3: {99, 42, 12, 32}, 4: {77, 66, 47}, 5: {65}}
)
def test_similarity_matrix_basic(self):
self.assertDictEqual(
similarity_matrix({1: {'a'}, 2: {'a'}}),
{1: {1: 1.0, 2: 1.0}, 2: {1: 1.0, 2: 1.0}}
)
self.assertDictEqual(
similarity_matrix({1: {'a'}, 2: {'b'}}),
{1: {1: 1.0, 2: 0.0}, 2: {1: 0.0, 2: 1.0}}
)
def test_similarity_matrix_elements_equal_to_themselves(self):
larger_list = {i: {i} for i in range(100)}
larger_list_matrix = similarity_matrix(larger_list)
self.assertEqual(len(larger_list_matrix), len(larger_list))
for i in range(len(larger_list)):
self.assertEqual(larger_list_matrix[i][i], 1.)
def test_similarity_matrix_with_testdata(self):
self.assertDictEqual(
similarity_matrix(read_file('testdata.csv')), {
1: {1: 1.0, 2: 0.16666666666666666, 3: 0.75, 4: 0.0, 5: 0.0},
2: {1: 0.16666666666666666, 2: 1.0, 3: 0.14285714285714285, 4: 0.4, 5: 0.0},
3: {1: 0.75, 2: 0.14285714285714285, 3: 1.0, 4: 0.0, 5: 0.0},
4: {1: 0.0, 2: 0.4, 3: 0.0, 4: 1.0, 5: 0.0},
5: {1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 1.0}
}
)
def test_similar_users(self):
similarity = similarity_matrix({1: {'a'}, 2: {'a'}})
self.assertEqual(similar_users(1, similarity, 0.2), [2])
self.assertEqual(similar_users(2, similarity, 0.2), [1])
self.assertEqual(similar_users(1, similarity, 1.0), [2])
similarity = similarity_matrix({1: {'a'}, 2: {'b'}})
self.assertEqual(similar_users(1, similarity, 0.2), [])
similarity = similarity_matrix(read_file('testdata.csv'))
self.assertEqual(similar_users(1, similarity, 0.2), [3])
self.assertEqual(similar_users(2, similarity, 0.15), [1, 4])
def test_recommendations(self):
sets = {1: {'a'}, 2: {'a', 'b'}}
similarity = similarity_matrix(sets)
self.assertEqual(recommendations(1, sets, similarity, 0.4), {'b'})
self.assertEqual(recommendations(2, sets, similarity, 0.4), set())
def test_recommendations_with_testdata(self):
sets = read_file('testdata.csv')
similarity = similarity_matrix(sets)
self.assertEqual(recommendations(1, sets, similarity, 0.75), {42})
self.assertFalse(recommendations(3, sets, similarity, 0.75))
self.assertEqual(
recommendations(1, sets, similarity, 0.15),
(sets[2] | sets[3]) - sets[1]
)
def test_recommendations_with_zero_cutoff_returns_all_other_products(self):
sets = read_file('testdata.csv')
similarity = similarity_matrix(sets)
for i in sets.keys():
self.assertEqual(
recommendations(i, sets, similarity, 0),
reduce(lambda a, b: a | b, sets.values(), set()) - sets[i]
)
def test_asymmetric_similarity(self):
self.assertEqual(asymmetric_similarity({'a'}, {'a', 'b'}), 1)
self.assertEqual(asymmetric_similarity({'a', 'b'}, {'a'}), 0.5)
sets = {1: {'a'}, 2: {'a', 'b'}}
similarity = similarity_matrix(sets, asymmetric_similarity)
self.assertDictEqual(
similarity, {1: {1: 1.0, 2: 1.0}, 2: {1: 0.5, 2: 1.0}}
)
def test_asymmetric_similarity_returns_superset_of_jaccard(self):
sets = read_file('testdata.csv')
similarity1 = similarity_matrix(sets)
similarity2 = similarity_matrix(sets, asymmetric_similarity)
for i in sets.keys():
self.assertTrue(
recommendations(i, sets, similarity1, 0.25).issubset(
recommendations(i, sets, similarity2, 0.25)
)
)
def test_minhashed_bounded_by_supplied_length(self):
self.assertEqual(minhashed({1}), {1})
self.assertEqual(minhashed({1}, 2), {1})
bigger_set = {i for i in range(100)}
self.assertLessEqual(len(minhashed(bigger_set, 10)), 10)
def test_minhash_similarity_succeeds_in_obvious_cases(self):
self.assertEqual(minhash_similarity({1, 2}, {3, 4}), 0.)
self.assertEqual(minhash_similarity({1, 2}, {2, 1}), 1.)
def test_minhash_with_strings(self):
self.assertEqual(
minhash(
[
("haoyuan", ["ak420", "ipad", "girlfriend"]),
("fenfen", ["ak46", "bayaji", "genjiu"])
],
5, 0
),
[
('haoyuan', ['ipad', 'girlfriend', 'ak420', 'ak420', 'ipad']),
('fenfen', ['ak46', 'bayaji', 'ak46', 'bayaji', 'bayaji'])
]
)
def test_minhash_with_ints(self):
self.assertEqual(
minhash(
[
(1, [12, 99, 32]),
(2, [32, 77, 54, 66]),
(3, [99, 42, 12, 32]),
(4, [77, 66, 47]),
(5, [65])
],
10, 0
),
[
(1, [99, 32, 12, 12, 99, 12, 99, 12, 99, 99]),
(2, [32, 66, 77, 77, 32, 66, 54, 54, 66, 32]),
(3, [32, 42, 32, 42, 99, 32, 32, 99, 12, 32]),
(4, [66, 47, 47, 66, 77, 66, 47, 47, 77, 47]),
(5, [65, 65, 65, 65, 65, 65, 65, 65, 65, 65])
]
)
def test_minhash_with_testdata(self):
sets = read_file('testdata.csv')
similarity = similarity_matrix(sets, minhash_similarity)
self.assertEqual(recommendations(1, sets, similarity, 0.75), {42})
self.assertFalse(recommendations(3, sets, similarity, 0.75))
self.assertEqual(
recommendations(1, sets, similarity, 0.15),
(sets[2] | sets[3]) - sets[1]
)
if __name__ == '__main__':
unittest.main()