-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_als_implicit.py
133 lines (98 loc) · 3.83 KB
/
test_als_implicit.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
import logging
import pickle
from lenskit import topn, util
from lenskit.algorithms import als
import pandas as pd
import numpy as np
from pytest import mark
import lenskit.util.test as lktu
_log = logging.getLogger(__name__)
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_als_basic_build():
algo = als.ImplicitMF(20, iterations=10, progress=util.no_progress)
algo.fit(simple_df)
assert set(algo.user_index_) == set([10, 12, 13])
assert set(algo.item_index_) == set([1, 2, 3])
assert algo.user_features_.shape == (3, 20)
assert algo.item_features_.shape == (3, 20)
def test_als_predict_basic():
algo = als.ImplicitMF(20, iterations=10)
algo.fit(simple_df)
preds = algo.predict_for_user(10, [3])
assert len(preds) == 1
assert preds.index[0] == 3
assert preds.loc[3] >= -0.1
assert preds.loc[3] <= 5
def test_als_predict_bad_item():
algo = als.ImplicitMF(20, iterations=10)
algo.fit(simple_df)
preds = algo.predict_for_user(10, [4])
assert len(preds) == 1
assert preds.index[0] == 4
assert np.isnan(preds.loc[4])
def test_als_predict_bad_user():
algo = als.ImplicitMF(20, iterations=10)
algo.fit(simple_df)
preds = algo.predict_for_user(50, [3])
assert len(preds) == 1
assert preds.index[0] == 3
assert np.isnan(preds.loc[3])
@lktu.wantjit
def test_als_train_large():
algo = als.ImplicitMF(20, iterations=20)
ratings = lktu.ml_test.ratings
algo.fit(ratings)
assert len(algo.user_index_) == ratings.user.nunique()
assert len(algo.item_index_) == ratings.item.nunique()
assert algo.user_features_.shape == (ratings.user.nunique(), 20)
assert algo.item_features_.shape == (ratings.item.nunique(), 20)
def test_als_save_load():
algo = als.ImplicitMF(20, iterations=5)
ratings = lktu.ml_test.ratings
algo.fit(ratings)
mod = pickle.dumps(algo)
_log.info('serialized to %d bytes', len(mod))
restored = pickle.loads(mod)
assert np.all(restored.user_features_ == algo.user_features_)
assert np.all(restored.item_features_ == algo.item_features_)
assert np.all(restored.item_index_ == algo.item_index_)
assert np.all(restored.user_index_ == algo.user_index_)
@lktu.wantjit
def test_als_train_large_noratings():
algo = als.ImplicitMF(20, iterations=20)
ratings = lktu.ml_test.ratings
ratings = ratings.loc[:, ['user', 'item']]
algo.fit(ratings)
assert len(algo.user_index_) == ratings.user.nunique()
assert len(algo.item_index_) == ratings.item.nunique()
assert algo.user_features_.shape == (ratings.user.nunique(), 20)
assert algo.item_features_.shape == (ratings.item.nunique(), 20)
@mark.slow
@mark.eval
@mark.skipif(not lktu.ml100k.available, reason='ML100K data not present')
def test_als_implicit_batch_accuracy():
import lenskit.crossfold as xf
from lenskit import batch
from lenskit import topn
ratings = lktu.ml100k.ratings
algo = als.ImplicitMF(25, iterations=20)
def eval(train, test):
_log.info('running training')
train['rating'] = train.rating.astype(np.float_)
algo.fit(train)
users = test.user.unique()
_log.info('testing %d users', len(users))
candidates = topn.UnratedCandidates(train)
recs = batch.recommend(algo, users, 100, candidates)
return recs
folds = list(xf.partition_users(ratings, 5, xf.SampleFrac(0.2)))
test = pd.concat(te for (tr, te) in folds)
recs = pd.concat(eval(train, test) for (train, test) in folds)
_log.info('analyzing recommendations')
rla = topn.RecListAnalysis()
rla.add_metric(topn.ndcg)
results = rla.compute(recs, test)
_log.info('nDCG for users is %.4f', results.ndcg.mean())
assert results.ndcg.mean() > 0