-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_recommendations.py
54 lines (40 loc) · 1.79 KB
/
get_recommendations.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
from __future__ import (absolute_import, division, print_function, unicode_literals)
from collections import defaultdict
from surprise import Dataset, Reader, evaluate, dump
import os, io
# load the trained and serialized model
predictions, algorithm = dump.load('serialized_model')
def resolve_ids_to_names():
# map usernames and movie names to their IDs in their respective files
users_file = os.path.expanduser('users.txt')
movies_file = os.path.expanduser('movies.txt')
movie_titles = {}
user_names = {}
with io.open(movies_file, 'r', encoding='ISO-8859-1') as movies:
for movie in movies:
movie = movie.split('|')
movie_titles[movie[0]] = movie[1]
with io.open(users_file, 'r', encoding='ISO-8859-1') as users:
for user in users:
user = user.split('|')
user_names[user[0]] = user[1]
return movie_titles, user_names
def get_recommendations(predictions, top_n=10):
# borrowed from http://surprise.readthedocs.io/en/stable/FAQ.html#how-to-get-the-top-n-recommendations-for-each-user
recommendations = defaultdict(list)
for uid, iid, true_r, est, _ in predictions:
recommendations[uid].append((iid, est))
for uid, user_ratings in recommendations.items():
user_ratings.sort(key=lambda x:x[1], reverse=True)
recommendations[uid] = user_ratings[:top_n]
return recommendations
recommendations = get_recommendations(predictions, top_n=5)
movie_titles, user_names = resolve_ids_to_names()
full_roster_recomendations = []
for uid, user_ratings in recommendations.items():
full_roster_recomendations.append((user_names[uid], [movie_titles[iid] for (iid, _) in user_ratings]))
userid = raw_input('Enter the userID or ALL:')
if userid == 'all':
print(full_roster_recomendations)
else:
print(full_roster_recomendations[int(userid)])