forked from CanBul/recompy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Similarities.py
90 lines (71 loc) · 3.21 KB
/
Similarities.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
import numpy as np
from numpy.linalg import norm
class Similarities():
@staticmethod
def get_most_similar_users(user_ratings, user_predictions, similarity_measure, howMany):
similarities = []
for user, ratings in user_predictions.items():
if similarity_measure == 'cosine_similarity':
similarity = Similarities.cosine_similarity(
list(user_ratings.values()), ratings)
elif similarity_measure == 'pearson_correlation':
similarity = Similarities.pearson_correlation(
list(user_ratings.values()), ratings)
elif similarity_measure == 'adjusted_cosine_similarity':
similarity = Similarities.adjusted_cosine_similarity(
list(user_ratings.values()), ratings)
elif similarity_measure == 'weighted_cosine_similarity':
similarity = Similarities.weighted_cosine_similarity(
list(user_ratings.values()), ratings)
elif similarity_measure == 'constrained_pearson_correlation':
similarity = Similarities.weighted_cosine_similarity(
list(user_ratings.values()), ratings)
elif similarity_measure == 'mean_squared_difference':
similarity = Similarities.mean_squared_difference(
list(user_ratings.values()), ratings)
elif similarity_measure == 'constrained_pearson_correlation':
similarity = Similarities.constrained_pearson_correlation(
list(user_ratings.values()), ratings)
similarities.append([user, similarity])
similarities.sort(reverse=True, key=lambda x: x[1])
return [each[0] for each in similarities[:howMany]]
@staticmethod
def cosine_similarity(a, b):
return np.dot(a, b)/(norm(a)*norm(b))
@staticmethod
def pearson_correlation(a, b):
corr = np.corrcoef(a, b)[0, 1]
return corr
@staticmethod
def weighted_cosine_similarity(a, b):
shared_item_count = len(a)
cosine_similarity = np.dot(a, b)/(norm(a)*norm(b))
weighted_cosine_similarity = cosine_similarity * \
(1 / (1+np.exp(-1*shared_item_count)))
return weighted_cosine_similarity
@staticmethod
def adjusted_cosine_similarity(a, b):
mean_response = sum(sum(a, b)) / (2*len(a))
a = a - mean_response
b = b - mean_response
return np.dot(a, b)/(norm(a)*norm(b))
@staticmethod
def mean_squared_difference(a, b):
summation = 0
n = len(a)
for i in range(0, n):
difference = a[i] - b[i]
squared_difference = difference**2
summation = summation + squared_difference
MSE = summation/n
return 1/MSE
@staticmethod
def constrained_pearson_correlation(a, b):
median_a = np.median(a)
median_b = np.median(b)
nominator = np.dot((a - median_a), (b - median_b))
denominator1 = np.sqrt(np.dot((a - median_a), (a - median_a)))
denominator2 = np.sqrt(np.dot((b - median_b), (b - median_b)))
denominator = denominator1 * denominator2
cpc = nominator / denominator
return cpc