-
Notifications
You must be signed in to change notification settings - Fork 1
/
Recommendare.py
152 lines (141 loc) · 5.83 KB
/
Recommendare.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
import time
import math
from operator import itemgetter
from collections import defaultdict
import common
from usersimilarity import UserSimilarity
from slopeone import SlopeOne
from user_wrapper import UserWrapper
class Recommendare:
def __init__(self):
self.user_similarity = UserSimilarity()
self.user_interface = UserWrapper()
self.slope_one = SlopeOne()
def _recommend_movies_list(self, user_id, neighbours, movies):
similarity = {n['user_id']: n['similarity'] for n in neighbours}
cosine_ratings = defaultdict(lambda: defaultdict(lambda: 0))
for m in movies:
cosine_ratings[m['movie_id']]['num'] += similarity[m['user_id']] * m['rating']
cosine_ratings[m['movie_id']]['den'] += similarity[m['user_id']]
return [{
'movie_id': p['movie_id'],
'predicted_rating': (p['rating'] + (cosine_ratings[p['movie_id']]['num']/cosine_ratings[p['movie_id']]['den']))/float(2)
} for p in self.slope_one.predict_ratings(user_id, movies)]
def serendipity_recommendation(self, user_id, count, knn=3):
neighbours = self.user_similarity.find_k_nearest(user_id, knn)
movies = list(common.users.aggregate([
{'$match': {'id': {'$in': [x['user_id'] for x in neighbours]}}},
{'$unwind': '$ratings'},
{'$sort': {'ratings.rating': -1}},
{'$limit': count * 10},
{'$sample': {'size': count}},
{'$project': {'_id': 0, 'movie_id': '$ratings.movie_id', 'rating': '$ratings.rating', 'user_id': '$id'}}
]))
return self._recommend_movies_list(user_id, neighbours, movies)
def best_recommendation(self, user_id, count, knn=3):
neighbours = self.user_similarity.find_k_nearest(user_id, knn)
movies = list(common.users.aggregate([
{'$match': {'id': {'$in': [x['user_id'] for x in neighbours]}}},
{'$unwind': '$ratings'},
{'$sort': {'ratings.rating': -1}},
{'$project': {'_id': 0, 'movie_id': '$ratings.movie_id', 'rating': '$ratings.rating', 'user_id': '$id'}}
]))
return sorted(self._recommend_movies_list(user_id, neighbours, movies), key=itemgetter('predicted_rating'), reverse=True)[:count]
def fast_recommendation(self, user_id, count, knn=3):
slicer = math.ceil(count/float(knn))
neighbours = self.user_similarity.find_k_nearest(user_id, knn)
movies = list(common.users.aggregate([
{'$project': {'ratings': {'$slice': ['$ratings', slicer]}, 'id': 1}},
{'$match': {'id': {'$in': [x['user_id'] for x in neighbours]}}},
{'$unwind': '$ratings'},
{'$sort': {'ratings.rating': -1}},
{'$project': {'_id': 0, 'movie_id': '$ratings.movie_id', 'rating': '$ratings.rating', 'user_id': '$id'}}
]))
return sorted(self._recommend_movies_list(user_id, neighbours, movies), key=itemgetter('predicted_rating'), reverse=True)[:count]
def predict_rating(self, user_id, movie_id, knn=3):
users = common.users.aggregate([{
'$match': {
'ratings.movie_id': 1,
'id': {
'$ne': 344
}
}
}, {
'$lookup': {
'from': 'user_similarity',
'localField': 'id',
'foreignField': 'user_id',
'as': 'similarity'
}
}, {
'$project': {
'_id': 0,
'id': 1,
'rating': {
'$let': {
'vars': {
'rr': {
'$arrayElemAt': [{
'$filter': {
'input': '$ratings',
'as': 'rating',
'cond': {
'$eq': ['$$rating.movie_id', 1]
}
}
}, 0]
}
},
'in': '$$rr.rating'
}
},
'similarity': {
'$let': {
'vars': {
'kk': {
'$let': {
'vars': {
'similarity': {
'$arrayElemAt': ["$similarity", 0]
}
},
'in': {
'$arrayElemAt': [{
'$filter': {
'input': '$$similarity.similarity',
'as': 'sim',
'cond': {
'$eq': ['$$sim.user_id', 344]
}
}
}, 0]
}
}
}
},
'in': '$$kk.similarity'
}
}
}
}, {
'$sort': {
'similarity': -1
}
}, {
'$limit': 5}])
slope = self.slope_one.predict_rating(user_id, movie_id)
den = 0.0
num = 0
for user in users:
den += user['similarity']
num += user['rating'] * user['similarity']
if den == 0.0:
return slope
return ((num / den) + slope) / 2
def register_user(self, user_data):
self.user_similarity.register_user(user_data)
def update_user_likes(self, user_id, likes):
self.self.user_similarity.update_user_likes(user_id, likes)
def user_rate_movie(self, user_id, movie_id, rating):
self.slope_one.update_deviation(user_id, movie_id, rating)
self.user_similarity.rate_movie(user_id, movie_id, rating)