forked from devAmoghS/Machine-Learning-with-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (45 loc) · 2.42 KB
/
Copy pathutils.py
File metadata and controls
65 lines (45 loc) · 2.42 KB
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
import math
from collections import defaultdict
from helpers.linear_algebra import dot
def cosine_similarity(v, w):
return dot(v, w) / math.sqrt(dot(v, v) * dot(w, w))
def make_user_interest_vector(interests, user_interests):
return [1 if interest in user_interests else 0
for interest in interests]
def most_similar_users_to(user_similarities, user_id):
pairs = [(other_user_id, similarity)
for other_user_id, similarity in
enumerate(user_similarities[user_id])
if user_id != other_user_id and similarity > 0]
return sorted(pairs, key=lambda pair: pair[1], reverse=True)
def most_similar_interests_to(interest_similarities, interest_id, unique_interests):
pairs = [(unique_interests[other_interest_id], similarity)
for other_interest_id, similarity in
enumerate(interest_similarities[interest_id])
if interest_id != other_interest_id and similarity > 0]
return sorted(pairs, key=lambda pair: pair[1], reverse=True)
def user_based_suggestions(user_similarities, users_interests, user_id, include_current_interests=False):
suggestions = defaultdict(float)
for other_user_id, similarity in most_similar_users_to(user_similarities, user_id):
for interest in users_interests[other_user_id]:
suggestions[interest] += similarity
suggestions = sorted(suggestions.items(), key=lambda pair: pair[1], reverse=True)
if include_current_interests:
return suggestions
else:
return [(suggestion, weight)
for suggestion, weight in suggestions
if suggestion not in users_interests[user_id]]
def item_based_suggestions(interest_similarities, users_interests, user_interest_matrix, unique_interests, user_id, include_current_interests=False):
suggestions = defaultdict(float)
for interest_id, is_interested in enumerate(user_interest_matrix[user_id]):
if is_interested == 1:
for interest, similarity in most_similar_interests_to(interest_similarities, interest_id, unique_interests):
suggestions[interest] += similarity
suggestions = sorted(suggestions.items(), key=lambda pair: pair[1], reverse=True)
if include_current_interests:
return suggestions
else:
return [(suggestion, weight)
for suggestion, weight in suggestions
if suggestion not in users_interests[user_id]]