-
Notifications
You must be signed in to change notification settings - Fork 0
/
collaborative_filtering.py
executable file
·465 lines (366 loc) · 12.4 KB
/
collaborative_filtering.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from pymongo import MongoClient
from bson.objectid import ObjectId
from scipy.linalg import norm
import numpy as np
client = MongoClient('localhost', 28017)
hubchat = client.hubchat
def split_ratings_training_testing():
hubchat.ratings_training.delete_many({})
hubchat.ratings_testing.delete_many({})
for user in hubchat.ratings.aggregate([
{
"$group": {
"_id": "$user",
"count": {"$sum": 1}
}
}
]):
ratings = list(hubchat.ratings.find({'user': user['_id']}).sort("createdAt"))
training_len = round(len(ratings) * 90 / 100)
training_set = ratings[:training_len]
testing_set = ratings[training_len:]
for rate in training_set:
hubchat.ratings_training.insert_one(
{
"user": rate['user'],
"post": rate['post'],
"rate": rate['rate'],
"createdAt": rate['createdAt']
}
)
for rate in testing_set:
hubchat.ratings_testing.insert_one(
{
"user": rate['user'],
"post": rate['post'],
"rate": rate['rate'],
"createdAt": rate['createdAt']
}
)
def split_ratings_training_validate():
hubchat.ratings_validate.delete_many({})
for user in hubchat.ratings_training.aggregate([
{
"$group": {
"_id": "$user",
"count": {"$sum": 1}
}
}
]):
ratings = list(hubchat.ratings_training.find({'user': user['_id']}).sort("createdAt"))
training_len = round(len(ratings) * 90 / 100)
validate_set = ratings[training_len:]
for rate in validate_set:
hubchat.ratings_validate.insert_one(
{
"user": rate['user'],
"post": rate['post'],
"rate": rate['rate'],
"createdAt": rate['createdAt']
}
)
hubchat.ratings_training.delete_many(
{
"user": rate['user'],
"post": rate['post']
}
)
def merge_ratings_training_validate():
for rate in hubchat.ratings_validate.find({}):
hubchat.ratings_training.insert_one(
{
"user": rate['user'],
"post": rate['post'],
"rate": rate['rate'],
"createdAt": rate['createdAt']
}
)
def get_rated_posts_except_user(user_id):
for post in hubchat.ratings_training.aggregate([
{
"$match": {
"rate": {
"$gt": 1
},
"user": {"$ne": ObjectId(user_id)}
}
},
{
"$group": {
"_id": "$post",
"users": {"$push": {"user": "$user", "rate": "$rate"}}
}
}
]):
yield {'_id': post['_id'], 'rates': {u['user']: u['rate'] for u in post['users']}}
def get_rated_posts(phase):
if phase == 1:
for post in hubchat.ratings_training.aggregate([
{
"$match": {
"rate": {
"$gt": 1
}
}
},
{
"$group": {
"_id": "$post",
"users": {"$push": {"user": "$user", "rate": "$rate"}}
}
}
]):
yield {'_id': post['_id'], 'rates': {u['user']: u['rate'] for u in post['users']}}
elif phase == 2:
for post in hubchat.ratings.aggregate([
{
"$match": {
"rate": {
"$gt": 1
}
}
},
{
"$group": {
"_id": "$post",
"users": {"$push": {"user": "$user", "rate": "$rate"}}
}
}
]):
yield {'_id': post['_id'], 'rates': {u['user']: u['rate'] for u in post['users']}}
def get_cosine_similarity(p1, p2):
if len(p2) < len(p1):
p1, p2 = p2, p1
res = 0
for key, p1_value in p1.items():
res += p1_value * p2.get(key, 0)
if res == 0:
return 0
try:
res = res / norm(list(p1.values())) / norm(list(p2.values()))
except ZeroDivisionError:
res = 0
return res
def update_similarities(phase):
# Delete all documents
hubchat.postsimilarity.delete_many({})
posts = list(get_rated_posts(phase))
posts2 = posts.copy()
skip = 0
analysed = set()
for post1 in posts:
skip += 1
for post2 in posts2[skip:]:
# Post 1 to be the smaller
post1_id, post2_id = sorted([str(post1['_id']), str(post2['_id'])])
# Skip if they are the same
if post1_id == post2_id:
continue
key = ''.join([post1_id, post2_id])
if key in analysed:
continue
analysed.add(key)
# Get similarity
sim = get_cosine_similarity(post1['rates'], post2['rates'])
if sim > 0:
# Insert on db
hubchat.postsimilarity.insert_one(
{
"post1": ObjectId(post1_id),
"post2": ObjectId(post2_id),
"similarity": sim
}
)
def get_users_with_positive_ratings():
return hubchat.ratings_training.aggregate([
{
"$match": {
"rate": {
"$gt": 1
}
}
},
{
"$group": {
"_id": "$user"
}
}
])
def update_similarities_per_user():
# Delete all documents
hubchat.postsimilarity.delete_many({})
for user in get_users_with_positive_ratings():
user_id = user['_id']
posts = list(get_rated_posts_except_user(user_id))
posts2 = posts.copy()
skip = 0
analysed = set()
for post1 in posts:
skip += 1
for post2 in posts2[skip:]:
# Post 1 to be the smaller
post1_id, post2_id = sorted([str(post1['_id']), str(post2['_id'])])
# Skip if they are the same
if post1_id == post2_id:
continue
key = ''.join([post1_id, post2_id])
if key in analysed:
continue
analysed.add(key)
# Get similarity
sim = get_cosine_similarity(post1['rates'], post2['rates'])
if sim > 0:
# Insert on db
hubchat.postsimilarity.insert_one(
{
"user": user_id,
"post1": ObjectId(post1_id),
"post2": ObjectId(post2_id),
"similarity": sim
}
)
def get_similar_posts(post_id, threshold):
sim_posts = []
for post in hubchat.postsimilarity.find({
"$or": [
{"post1": ObjectId(post_id)},
{"post2": ObjectId(post_id)}
],
"similarity": {
"$gte": threshold
}
}):
sim_post = str(post['post2']) if str(post['post1']) == str(post_id) else str(post['post1'])
sim_posts.append(sim_post)
return sim_posts
def get_high_rated_posts(user_id):
for rate in hubchat.ratings_training.find({
"user": ObjectId(user_id),
"rate": {"$gte": 3}
}):
yield str(rate['post'])
def get_recommendations(user_id, alpha):
threshold = define_threshold(alpha)
# Get posts similar to high rated posts
similars = set()
for post_id in get_high_rated_posts(user_id):
similars |= set(get_similar_posts(post_id, threshold))
# Remove posts already seen
for rate in hubchat.ratings_training.find({"user": ObjectId(user_id)}):
post_id = str(rate["post"])
similars.discard(post_id)
return similars
def get_recommendations_all(user_id, alpha):
threshold = define_threshold(alpha)
# Get posts similar to high rated posts
similars = set()
for post_id in get_high_rated_posts(user_id):
similars |= set(get_similar_posts(post_id, threshold))
# Remove posts already seen
for rate in hubchat.ratings.find({"user": ObjectId(user_id)}):
post_id = str(rate["post"])
similars.discard(post_id)
return similars
def define_threshold(alpha):
similarities = []
for post in hubchat.postsimilarity.find({}):
similarities.append(float(post['similarity']))
np_arr = np.array(similarities)
return np.average(np_arr) + (alpha * np.std(np_arr))
def get_confusion_matrix(user_id, recommendation_list, phase):
"""
Build confusion matrix for user
:param user_id:
:param recommendation_list:
:param phase:
:return:
"""
tp = 0 # True positive
fp = 0 # False positive
fn = 0 # False negative
tn = 0 # True negative
if recommendation_list is None:
recommendation_list = set()
for post_id in recommendation_list:
query = {
"user": ObjectId(user_id),
"post": ObjectId(post_id)
}
if phase == 1:
rate = hubchat.ratings_validate.find_one(query)
elif phase == 2:
rate = hubchat.ratings_testing.find_one(query)
if rate is None:
continue # Ignore
if 3 <= rate['rate'] <= 4:
tp += 1 # In recommendation list and in testing set with rate >= 3
elif 1 <= rate['rate'] <= 2:
fp += 1 # In recommendation list and in testing set with rate < 3
if phase == 1:
testing_rates = hubchat.ratings_validate.find({"user": ObjectId(user_id)})
elif phase == 2:
testing_rates = hubchat.ratings_testing.find({"user": ObjectId(user_id)})
for rate in testing_rates:
if str(rate['post']) in recommendation_list:
continue # Already analysed
if 3 <= rate['rate'] <= 4:
fn += 1 # In testing set with rate >= 3 but not in recommendation list
elif 1 <= rate['rate'] <= 2:
tn += 1 # In testing set with rate < 3 and not in recommendation list
return tp, fp, fn, tn
#
# split_ratings_training_testing()
#
# correct_rates = []
# # correct_recommendations = {}
# for user in hubchat.ratings_training.aggregate(users_with_positive_ratings_order_by_count):
# user_id = str(user['_id'])
# recommendation_list = get_recommendations(user_id)
#
# tp = 0 # True positive
# fp = 0 # False positive
#
# for post_id in recommendation_list:
# rate = hubchat.ratings_testing.find_one({
# "user": ObjectId(user_id),
# "post": ObjectId(post_id)
# })
#
# if rate is None:
# # Ignore
# continue
#
# if 3 <= rate['rate'] <= 4:
# tp += 1
# elif 1 <= rate['rate'] <= 2:
# fp += 1
#
# fn = 0 # False negative
# tn = 0 # True negative
# # try:
# correct_rate = count_correct / float(count)
# except ZeroDivisionError:
# correct_rate = None
#
# if correct_rate is not None:
# correct_rates.append(correct_rate)
#
# try:
# correct_recommendations[user['count']].append(count_correct)
# except KeyError:
# correct_recommendations[user['count']] = [count_correct]
#
# print("Positive ratings: %s , Recommendations unknown tried: %s , Recommendations known tried: %s , Correct: %s , correct rate: %s" % (user['count'], count_1, count, count_correct, correct_rate))
# print("Correct rates avg with a=0.75 -> threshold=0.857705843546: ", np.average(correct_rates))
# correct_recommendations = {k: np.average(v) for k, v in correct_recommendations.items()}
# print(correct_recommendations)
#
# import matplotlib.pyplot as plt
#
# fig, ax = plt.subplots()
# ax.bar(list(correct_recommendations.keys()), list(correct_recommendations.values()), width=1.0)
#
# ax.set_xlabel('Positive ratings')
# ax.set_ylabel('Correct recommendations')
# fig.tight_layout()
# plt.savefig("figures/cf-threshold-926009457076.pdf", format='pdf')