generated from lenskit/lk-demo-experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommend.py
executable file
·50 lines (39 loc) · 1.1 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
49
50
#!/usr/bin/env python3
"""
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
"""
import gzip
import pickle
from docopt import docopt
from lenskit.util import Stopwatch
from lkpm import datasets, log
_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)