-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetrics.py
311 lines (280 loc) · 11.4 KB
/
Metrics.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
import pandas as pd
import numpy as np
from scipy import spatial
from Two_tower_SVD_NN_Recommender import show
# from Memory_based_Recommender import show
# from MF_Biases_Recommender import show
# from MF_NN_Recommender import show
# from NN_Recommender import show
# from MF_NN_Two_Model_Hybrid_Recommender import show
# from SVD_Netflix_Recommender import show
# from SVD_item_item_Recommender import show
# from KNN_Recommender import show
# import data
df_movies = pd.read_csv('movies.csv', usecols=['movieId', 'title', 'genres'],
dtype={'movieId': 'int32', 'title': 'str', 'genres': 'str'})
df_ratings = pd.read_csv('ratings.csv', usecols=['userId', 'movieId', 'rating'],
dtype={'userId': 'int32', 'movieId': 'int32', 'rating': 'float32'})
# add a new column for category id
df_ratings.insert(2, "movieId_cat", (df_ratings.movieId.astype('category').cat.codes.values), True)
df_ratings.userId = df_ratings.userId.astype('category').cat.codes.values
users_movies = df_ratings.pivot(index='userId', columns='movieId', values='rating').fillna(0)
# create a map dataframe for movieIds
d = {'movieId_cat': df_ratings.movieId_cat.unique(), 'movieId': df_ratings.movieId.unique()}
df_movieId_map = pd.DataFrame(d)
# delete the non-sequential column of movieIds
df_ratings.drop('movieId_cat', axis=1, inplace=True)
# genres = np.empty(len(df_ratings), dtype=object)
# for i in range(len(df_ratings)):
# for j in range(len(df_movies)):
# if df_ratings.movieId[i]==df_movies.movieId[j]:
# genres[i] = df_movies.genres[j]
# continue
# df_ratings.insert(3,"genres",genres,True)
df_ratings_genres = pd.read_csv('ratings_genres.csv', usecols=['userId', 'movieId', 'rating', 'genres'],
dtype={'userId': 'int32', 'movieId': 'int32', 'rating': 'float32', 'genres': 'str'})
# find percentage of genres in each user
# init variables
def Percentages_ratings(target_user):
Adventure = 0
Comedy = 0
Drama = 0
Action = 0
Romance = 0
Mystery = 0
Crime = 0
War = 0
Animation = 0
Children = 0
Thriller = 0
Sci_Fi = 0
Western = 0
Fantasy = 0
Musical = 0
OverAll = 0
for i in range(len(df_ratings_genres)):
if df_ratings_genres.userId[i] == target_user:
if df_ratings_genres.genres[i].__contains__('Adventure'):
Adventure = Adventure + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Comedy'):
Comedy = Comedy + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Action'):
Action = Action + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Romance'):
Romance = Romance + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Mystery'):
Mystery = Mystery + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Crime'):
Crime = Crime + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('War'):
War = War + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Animation'):
Animation = Animation + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Children'):
Children = Children + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Thriller'):
Thriller = Thriller + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Sci_Fi'):
Sci_Fi = Sci_Fi + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Western'):
Western = Western + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Fantasy'):
Fantasy = Fantasy + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Musical'):
Musical = Musical + 1
OverAll = OverAll + 1
if df_ratings_genres.genres[i].__contains__('Drama'):
Drama = Drama + 1
OverAll = OverAll + 1
Adventure_p = Adventure / OverAll
Drama_p = Drama / OverAll
Comedy_p = Comedy / OverAll
Action_p = Action / OverAll
Romance_p = Romance / OverAll
Mystery_p = Mystery / OverAll
Crime_p = Crime / OverAll
War_p = War / OverAll
Animation_p = Animation / OverAll
Children_p = Children / OverAll
Thriller_p = Thriller / OverAll
Sci_Fi_p = Sci_Fi / OverAll
Western_p = Western / OverAll
Fantasy_p = Fantasy / OverAll
Musical_p = Musical / OverAll
data = {'Adventure': [Adventure_p], 'Drama': [Drama_p], 'Comedy': [Comedy_p], 'Action': [Action_p],
'Romance': [Romance_p], 'Mystery': [Mystery_p], 'Crime': [Crime_p], 'War': [War_p],
'Animation': [Animation_p], 'Children': [Children_p], 'Thriller': [Thriller_p], 'Sci_Fi': [Sci_Fi_p],
'Western': [Western_p], 'Fantasy': [Fantasy_p], 'Musical': [Musical_p]}
ind = {'Percentages'}
df = pd.DataFrame(data, index=ind)
return df
def Percentages_recommendations(target_user):
recommendations = show(target_user)
Adventure = 0
Drama = 0
Comedy = 0
Action = 0
Romance = 0
Mystery = 0
Crime = 0
War = 0
Animation = 0
Children = 0
Thriller = 0
Sci_Fi = 0
Western = 0
Fantasy = 0
Musical = 0
OverAll = 0
for i in range(len(recommendations)):
if recommendations[i][1].__contains__('Adventure'):
Adventure = Adventure + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Comedy'):
Comedy = Comedy + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Action'):
Action = Action + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Romance'):
Romance = Romance + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Mystery'):
Mystery = Mystery + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Crime'):
Crime = Crime + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('War'):
War = War + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Animation'):
Animation = Animation + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Children'):
Children = Children + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Thriller'):
Thriller = Thriller + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Sci_Fi'):
Sci_Fi = Sci_Fi + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Western'):
Western = Western + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Fantasy'):
Fantasy = Fantasy + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Musical'):
Musical = Musical + 1
OverAll = OverAll + 1
if recommendations[i][1].__contains__('Drama'):
Drama = Drama + 1
OverAll = OverAll + 1
Adventure_p = Adventure / OverAll
Drama_p = Drama / OverAll
Comedy_p = Comedy / OverAll
Action_p = Action / OverAll
Romance_p = Romance / OverAll
Mystery_p = Mystery / OverAll
Crime_p = Crime / OverAll
War_p = War / OverAll
Animation_p = Animation / OverAll
Children_p = Children / OverAll
Thriller_p = Thriller / OverAll
Sci_Fi_p = Sci_Fi / OverAll
Western_p = Western / OverAll
Fantasy_p = Fantasy / OverAll
Musical_p = Musical / OverAll
data = {'Adventure': [Adventure_p], 'Drama': [Drama_p], 'Comedy': [Comedy_p], 'Action': [Action_p],
'Romance': [Romance_p], 'Mystery': [Mystery_p], 'Crime': [Crime_p], 'War': [War_p],
'Animation': [Animation_p], 'Children': [Children_p], 'Thriller': [Thriller_p], 'Sci_Fi': [Sci_Fi_p],
'Western': [Western_p], 'Fantasy': [Fantasy_p], 'Musical': [Musical_p]}
ind = {'Percentages'}
df = pd.DataFrame(data, index=ind)
return df
# similarity between the percentages of genres
def Personalization(target_user):
df_1 = Percentages_ratings(target_user)
df_2 = Percentages_recommendations(target_user)
vector_1 = df_1.to_numpy()
vector_2 = df_2.to_numpy()
cosine_similarity = 1 - spatial.distance.cosine(vector_1, vector_2)
return cosine_similarity
similarities_matrix = np.empty(users_movies.shape[0])
for i in range(users_movies.shape[0]):
similarities_matrix[i] = Personalization(i)
mean_pers = similarities_matrix.mean()
def Similarity(target_user):
i = 0
s = 0
d = 0
recommended = show(target_user)
recommended_movieIds = recommended[:, 2]
n = len(recommended_movieIds)
while i < len(recommended_movieIds):
j = i
while j < len(recommended_movieIds):
cosine_similarity = 1 - spatial.distance.cosine(users_movies[:][recommended_movieIds[i]],
users_movies[:][recommended_movieIds[j]])
s = s + cosine_similarity
d = d + (1 - cosine_similarity)
j = j + 1
i = i + 1
S = s / ((n / 2) * (n - 1))
D = d / ((n / 2) * (n - 1))
return D
Diversity_matrix = np.empty(users_movies.shape[0])
for i in range(users_movies.shape[0]):
Diversity_matrix[i] = Similarity(i)
mean_div = Diversity_matrix.mean()
def All_recommendations():
recommendations_df = pd.DataFrame(index=range(users_movies.shape[0] * 10),
columns=['UserId', 'Title', 'Genre', 'MovieId'])
start = 0
step = 10
for i in range(users_movies.shape[0]):
end = start + step
recommendations = show(i)
recommendations_df.UserId[start:end] = i
recommendations_df.Title[start:end] = recommendations[:, 0]
recommendations_df.Genre[start:end] = recommendations[:, 1]
recommendations_df.MovieId[start:end] = recommendations[:, 2]
start = start + step
return recommendations_df
df = All_recommendations()
dups_movieId = df.pivot_table(index=['MovieId'], aggfunc='size')
def Novelty(target_user):
k = 0
l = 0
idx = np.empty(10)
nov = np.empty(10)
n = len(df.UserId.unique())
for i in range(len(df)):
if df.UserId[i] == target_user:
idx[k] = i
k = k +1
for j in idx:
count = dups_movieId[df.MovieId[j]]
nov[l] = 1 - (count/n)
l = l + 1
mean_nov = nov.mean()
return mean_nov
Novelty_matrix = np.empty(users_movies.shape[0])
for i in range(users_movies.shape[0]):
Novelty_matrix[i] = Novelty(i)
mean_nov = Novelty_matrix.mean()