-
Notifications
You must be signed in to change notification settings - Fork 5
/
recommend.py
48 lines (38 loc) · 1.08 KB
/
recommend.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
"""
Generate recommendations.
Usage:
recommend.py [-n N] [-d DS] MODEL USER ...
Options:
-n N the number of recommendations to produce [default: 10]
-d DS the data set, for reading item information
MODEL the name of the model file
USER the user IDs to recommend for
"""
from docopt import docopt
import gzip
import pickle
from lkdemo import datasets, log
from lenskit.util import Stopwatch
_log = log.script(__file__)
args = docopt(__doc__)
n = int(args['-n'])
if args['-d']:
_log.info('using data %s', args['-d'])
data = getattr(datasets, args['-d'])
items = data.movies
else:
data = None
items = None
_log.info('reading from %s', args['MODEL'])
with gzip.open(args['MODEL'], 'rb') as f:
algo = pickle.load(f)
for u in args['USER']:
u = int(u)
timer = Stopwatch()
_log.info('getting %d recs for user %d', n, u)
recs = algo.recommend(u, n)
if items is not None:
recs = recs.join(items, how='left', on='item')
print('recommendations for', u)
print(recs)
_log.info('completed recommendations in %s', timer)