-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_basic.py
367 lines (270 loc) · 10.5 KB
/
test_basic.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from lenskit.algorithms import basic
from lenskit import util as lku
import pandas as pd
import numpy as np
import pickle
import lenskit.util.test as lktu
from pytest import approx
simple_df = pd.DataFrame({'item': [1, 1, 2, 3],
'user': [10, 12, 10, 13],
'rating': [4.0, 3.0, 5.0, 2.0]})
def test_memorized():
algo = basic.Memorized(simple_df)
preds = algo.predict_for_user(10, [1, 2])
assert set(preds.index) == set([1, 2])
assert all(preds == pd.Series({1: 4.0, 2: 5.0}))
preds = algo.predict_for_user(12, [1, 3])
assert set(preds.index) == set([1, 3])
assert preds.loc[1] == 3.0
assert np.isnan(preds.loc[3])
def test_memorized_batch():
algo = basic.Memorized(simple_df)
preds = algo.predict(pd.DataFrame({'user': [10, 10, 12], 'item': [1, 2, 1]}))
assert isinstance(preds, pd.Series)
assert preds.name == 'prediction'
assert set(preds.index) == set([0, 1, 2])
assert all(preds == [4.0, 5.0, 3.0])
def test_memorized_batch_ord():
algo = basic.Memorized(simple_df)
preds = algo.predict(pd.DataFrame({'user': [10, 12, 10], 'item': [1, 1, 2]}))
assert set(preds.index) == set([0, 1, 2])
assert all(preds == [4.0, 3.0, 5.0])
def test_memorized_batch_missing():
algo = basic.Memorized(simple_df)
preds = algo.predict(pd.DataFrame({'user': [10, 12, 12], 'item': [1, 1, 3]}))
assert set(preds.index) == set([0, 1, 2])
assert all(preds.iloc[:2] == [4.0, 3.0])
assert np.isnan(preds.iloc[2])
def test_memorized_batch_keep_index():
algo = basic.Memorized(simple_df)
query = pd.DataFrame({'user': [10, 10, 12], 'item': [1, 2, 1]},
index=np.random.choice(np.arange(10), 3, False))
preds = algo.predict(query)
assert all(preds.index == query.index)
assert all(preds == [4.0, 5.0, 3.0])
def test_fallback_train_one():
algo = basic.Fallback(basic.Bias())
algo.fit(lktu.ml_test.ratings)
assert len(algo.algorithms) == 1
assert isinstance(algo.algorithms[0], basic.Bias)
assert algo.algorithms[0].mean_ == approx(lktu.ml_test.ratings.rating.mean())
def test_fallback_train_one_pred_impossible():
algo = basic.Fallback(basic.Memorized(simple_df))
algo.fit(lktu.ml_test.ratings)
preds = algo.predict_for_user(10, [1, 2])
assert set(preds.index) == set([1, 2])
assert all(preds == pd.Series({1: 4.0, 2: 5.0}))
preds = algo.predict_for_user(12, [1, 3])
assert set(preds.index) == set([1, 3])
assert preds.loc[1] == 3.0
assert np.isnan(preds.loc[3])
def test_fallback_list():
algo = basic.Fallback([basic.Memorized(simple_df), basic.Bias()])
algo.fit(lktu.ml_test.ratings)
assert len(algo.algorithms) == 2
params = algo.get_params()
assert list(params.keys()) == ['algorithms']
assert len(params['algorithms']) == 2
assert isinstance(params['algorithms'][0], basic.Memorized)
assert isinstance(params['algorithms'][1], basic.Bias)
def test_fallback_string():
algo = basic.Fallback([basic.Memorized(simple_df), basic.Bias()])
assert 'Fallback' in str(algo)
def test_fallback_clone():
algo = basic.Fallback([basic.Memorized(simple_df), basic.Bias()])
algo.fit(lktu.ml_test.ratings)
assert len(algo.algorithms) == 2
clone = lku.clone(algo)
assert clone is not algo
for a1, a2 in zip(algo.algorithms, clone.algorithms):
assert a1 is not a2
assert type(a2) == type(a1)
def test_fallback_predict():
algo = basic.Fallback(basic.Memorized(simple_df), basic.Bias())
algo.fit(lktu.ml_test.ratings)
assert len(algo.algorithms) == 2
bias = algo.algorithms[1]
assert isinstance(bias, basic.Bias)
assert bias.mean_ == approx(lktu.ml_test.ratings.rating.mean())
def exp_val(user, item):
v = bias.mean_
if user is not None:
v += bias.user_offsets_.loc[user]
if item is not None:
v += bias.item_offsets_.loc[item]
return v
# first user + item
preds = algo.predict_for_user(10, [1])
assert preds.loc[1] == 4.0
# second user + first item
preds = algo.predict_for_user(15, [1])
assert preds.loc[1] == approx(exp_val(15, 1))
# second item + user item
preds = algo.predict_for_user(12, [2])
assert preds.loc[2] == approx(exp_val(12, 2))
# blended
preds = algo.predict_for_user(10, [1, 5])
assert preds.loc[1] == 4.0
assert preds.loc[5] == approx(exp_val(10, 5))
# blended unknown
preds = algo.predict_for_user(10, [5, 1, -23081])
assert len(preds) == 3
assert preds.loc[1] == 4.0
assert preds.loc[5] == approx(exp_val(10, 5))
assert preds.loc[-23081] == approx(exp_val(10, None))
def test_fallback_save_load(tmp_path):
original = basic.Fallback(basic.Memorized(simple_df), basic.Bias())
original.fit(lktu.ml_test.ratings)
fn = tmp_path / 'fb.mod'
with fn.open('wb') as f:
pickle.dump(original, f)
with fn.open('rb') as f:
algo = pickle.load(f)
bias = algo.algorithms[1]
assert bias.mean_ == approx(lktu.ml_test.ratings.rating.mean())
def exp_val(user, item):
v = bias.mean_
if user is not None:
v += bias.user_offsets_.loc[user]
if item is not None:
v += bias.item_offsets_.loc[item]
return v
# first user + item
preds = algo.predict_for_user(10, [1])
assert preds.loc[1] == 4.0
# second user + first item
preds = algo.predict_for_user(15, [1])
assert preds.loc[1] == approx(exp_val(15, 1))
# second item + user item
preds = algo.predict_for_user(12, [2])
assert preds.loc[2] == approx(exp_val(12, 2))
# blended
preds = algo.predict_for_user(10, [1, 5])
assert preds.loc[1] == 4.0
assert preds.loc[5] == approx(exp_val(10, 5))
# blended unknown
preds = algo.predict_for_user(10, [5, 1, -23081])
assert len(preds) == 3
assert preds.loc[1] == 4.0
assert preds.loc[5] == approx(exp_val(10, 5))
assert preds.loc[-23081] == approx(exp_val(10, None))
def test_topn_recommend():
pred = basic.Memorized(simple_df)
rec = basic.TopN(pred)
rec.fit(simple_df)
rec10 = rec.recommend(10, candidates=[1, 2])
assert all(rec10.item == [2, 1])
assert all(rec10.score == [5, 4])
rec2 = rec.recommend(12, candidates=[1, 2])
assert len(rec2) == 1
assert all(rec2.item == [1])
assert all(rec2.score == [3])
rec10 = rec.recommend(10, n=1, candidates=[1, 2])
assert len(rec10) == 1
assert all(rec10.item == [2])
assert all(rec10.score == [5])
def test_topn_config():
pred = basic.Memorized(simple_df)
rec = basic.TopN(pred)
rs = str(rec)
assert rs.startswith('TopN/')
def test_topn_big():
ratings = lktu.ml_test.ratings
users = ratings.user.unique()
items = ratings.item.unique()
user_items = ratings.set_index('user').item
algo = basic.TopN(basic.Bias())
a2 = algo.fit(ratings)
assert a2 is algo
# test 100 random users
for u in np.random.choice(users, 100, False):
recs = algo.recommend(u, 100)
assert len(recs) == 100
rated = user_items.loc[u]
assert all(~recs['item'].isin(rated))
unrated = np.setdiff1d(items, rated)
scores = algo.predictor.predict_for_user(u, unrated)
top = scores.nlargest(100)
assert top.values == approx(recs.score.values)
def test_popular():
algo = basic.Popular()
algo.fit(lktu.ml_test.ratings)
counts = lktu.ml_test.ratings.groupby('item').user.count()
counts = counts.nlargest(100)
assert algo.item_pop_.max() == counts.max()
recs = algo.recommend(2038, 100)
assert len(recs) == 100
assert all(np.diff(recs.score) <= 0)
assert recs.score.iloc[0] == counts.max()
# the 10 most popular should be the same
assert all(counts.index[:10] == recs.item[:10])
def test_popular_excludes_rated():
algo = basic.Popular()
algo.fit(lktu.ml_test.ratings)
counts = lktu.ml_test.ratings.groupby('item').user.count()
counts = counts.nlargest(100)
recs = algo.recommend(100, 100)
assert len(recs) == 100
assert all(np.diff(recs.score) <= 0)
# make sure we didn't recommend anything the user likes
ratings = lktu.ml_test.ratings
urates = ratings.set_index(['user', 'item'])
urates = urates.loc[100, :]
match = recs.join(urates, on='item', how='inner')
assert len(match) == 0
def test_pop_candidates():
algo = basic.Popular()
algo.fit(lktu.ml_test.ratings)
counts = lktu.ml_test.ratings.groupby('item').user.count()
items = lktu.ml_test.ratings.item.unique()
assert algo.item_pop_.max() == counts.max()
candidates = np.random.choice(items, 500, replace=False)
recs = algo.recommend(2038, 100, candidates)
assert len(recs) == 100
assert all(np.diff(recs.score) <= 0)
ccs = counts.loc[candidates]
ccs = ccs.sort_values(ascending=False)
assert recs.score.iloc[0] == ccs.max()
equiv = ccs[ccs == ccs.max()]
assert recs.item.iloc[0] in equiv.index
def test_pop_save_load():
original = basic.Popular()
original.fit(lktu.ml_test.ratings)
mod = pickle.dumps(original)
algo = pickle.loads(mod)
counts = lktu.ml_test.ratings.groupby('item').user.count()
counts = counts.nlargest(100)
assert algo.item_pop_.max() == counts.max()
recs = algo.recommend(2038, 100)
assert len(recs) == 100
assert all(np.diff(recs.score) <= 0)
assert recs.score.iloc[0] == counts.max()
# the 10 most popular should be the same
assert all(counts.index[:10] == recs.item[:10])
def test_unrated_selector():
sel = basic.UnratedItemCandidateSelector()
s2 = sel.fit(simple_df)
assert s2 is sel
assert set(sel.candidates(10)) == set([3])
assert set(sel.candidates(12)) == set([3, 2])
assert set(sel.candidates(11)) == set([1, 2, 3])
def test_unrated_override():
sel = basic.UnratedItemCandidateSelector()
sel.fit(simple_df)
assert set(sel.candidates(10, [2])) == set([1, 3])
def test_unrated_big():
ratings = lktu.ml_test.ratings
users = ratings.user.unique()
items = ratings.item.unique()
user_items = ratings.set_index('user').item
sel = basic.UnratedItemCandidateSelector()
s2 = sel.fit(ratings)
assert s2 is sel
# test 100 random users
for u in np.random.choice(users, 100, False):
candidates = sel.candidates(u)
candidates = pd.Series(candidates)
uis = user_items.loc[u]
assert len(uis) + len(candidates) == len(items)
assert candidates.nunique() == len(candidates)
assert all(~candidates.isin(uis))