-
Notifications
You must be signed in to change notification settings - Fork 100
/
metrics.py
438 lines (392 loc) · 14.7 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
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
import random
from itertools import product
from math import sqrt
from typing import List
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.sparse as sp
from sklearn.metrics import confusion_matrix, mean_squared_error
from sklearn.metrics.pairwise import cosine_similarity
import warnings
def novelty(predicted: List[list], pop: dict, u: int, n: int) -> (float, list):
"""
Computes the novelty for a list of recommendations
Parameters
----------
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
pop: dictionary
A dictionary of all items alongside of its occurrences counter in the training data
example: {1198: 893, 1270: 876, 593: 876, 2762: 867}
u: integer
The number of users in the training data
n: integer
The length of recommended lists per user
Returns
----------
novelty:
The novelty of the recommendations in system level
mean_self_information:
The novelty of the recommendations in recommended top-N list level
----------
Metric Defintion:
Zhou, T., Kuscsik, Z., Liu, J. G., Medo, M., Wakeling, J. R., & Zhang, Y. C. (2010).
Solving the apparent diversity-accuracy dilemma of recommender systems.
Proceedings of the National Academy of Sciences, 107(10), 4511-4515.
"""
mean_self_information = []
k = 0
for sublist in predicted:
self_information = 0
k += 1
for i in sublist:
self_information += np.sum(-np.log2(pop[i]/u))
mean_self_information.append(self_information/n)
novelty = sum(mean_self_information)/k
return novelty, mean_self_information
def prediction_coverage(predicted: List[list], catalog: list, unseen_warning: bool=False) -> float:
"""
Computes the prediction coverage for a list of recommendations
Parameters
----------
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
catalog: list
A list of all unique items in the training data
example: ['A', 'B', 'C', 'X', 'Y', Z]
unseen_warn: bool
when prediction gives any item unseen in catalog:
(1) ignore the unseen item and warn
(2) or raise an exception.
Returns
----------
prediction_coverage:
The prediction coverage of the recommendations as a percent
rounded to 2 decimal places
----------
Metric Defintion:
Ge, M., Delgado-Battenfeld, C., & Jannach, D. (2010, September).
Beyond accuracy: evaluating recommender systems by coverage and serendipity.
In Proceedings of the fourth ACM conference on Recommender systems (pp. 257-260). ACM.
"""
unique_items_catalog = set(catalog)
if len(catalog)!=len(unique_items_catalog):
raise AssertionError("Duplicated items in catalog")
predicted_flattened = [p for sublist in predicted for p in sublist]
unique_items_pred = set(predicted_flattened)
if not unique_items_pred.issubset(unique_items_catalog):
if unseen_warning:
warnings.warn("There are items in predictions but unseen in catalog. "
"They are ignored from prediction_coverage calculation")
unique_items_pred = unique_items_pred.intersection(unique_items_catalog)
else:
raise AssertionError("There are items in predictions but unseen in catalog.")
num_unique_predictions = len(unique_items_pred)
prediction_coverage = round(num_unique_predictions/(len(catalog)* 1.0)* 100, 2)
return prediction_coverage
def catalog_coverage(predicted: List[list], catalog: list, k: int) -> float:
"""
Computes the catalog coverage for k lists of recommendations
Parameters
----------
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
catalog: list
A list of all unique items in the training data
example: ['A', 'B', 'C', 'X', 'Y', Z]
k: integer
The number of observed recommendation lists
which randomly choosed in our offline setup
Returns
----------
catalog_coverage:
The catalog coverage of the recommendations as a percent
rounded to 2 decimal places
----------
Metric Defintion:
Ge, M., Delgado-Battenfeld, C., & Jannach, D. (2010, September).
Beyond accuracy: evaluating recommender systems by coverage and serendipity.
In Proceedings of the fourth ACM conference on Recommender systems (pp. 257-260). ACM.
"""
sampling = random.choices(predicted, k=k)
predicted_flattened = [p for sublist in sampling for p in sublist]
L_predictions = len(set(predicted_flattened))
catalog_coverage = round(L_predictions/(len(catalog)*1.0)*100,2)
return catalog_coverage
def _ark(actual: list, predicted: list, k=10) -> float:
"""
Computes the average recall at k.
Parameters
----------
actual : list
A list of actual items to be predicted
predicted : list
An ordered list of predicted items
k : int, default = 10
Number of predictions to consider
Returns:
-------
score : float
The average recall at k.
"""
if len(predicted)>k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits / (i+1.0)
if not actual:
return 0.0
return score / len(actual)
def _apk(actual: list, predicted: list, k=10) -> float:
"""
Computes the average precision at k.
Parameters
----------
actual : list
A list of actual items to be predicted
predicted : list
An ordered list of predicted items
k : int, default = 10
Number of predictions to consider
Returns:
-------
score : float
The average precision at k.
"""
if not predicted or not actual:
return 0.0
if len(predicted) > k:
predicted = predicted[:k]
score = 0.0
true_positives = 0.0
for i, p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
max_ix = min(i + 1, len(predicted))
score += _precision(predicted[:max_ix], actual)
true_positives += 1
if score == 0.0:
return 0.0
return score / true_positives
def mark(actual: List[list], predicted: List[list], k=10) -> float:
"""
Computes the mean average recall at k.
Parameters
----------
actual : a list of lists
Actual items to be predicted
example: [['A', 'B', 'X'], ['A', 'B', 'Y']]
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
Returns:
-------
mark: float
The mean average recall at k (mar@k)
"""
if len(actual) != len(predicted):
raise AssertionError("Length mismatched")
return np.mean([_ark(a,p,k) for a,p in zip(actual, predicted)])
def mapk(actual: List[list], predicted: List[list], k: int=10) -> float:
"""
Computes the mean average precision at k.
Parameters
----------
actual : a list of lists
Actual items to be predicted
example: [['A', 'B', 'X'], ['A', 'B', 'Y']]
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
Returns:
-------
mark: float
The mean average precision at k (map@k)
"""
if len(actual) != len(predicted):
raise AssertionError("Length mismatched")
return np.mean([_apk(a,p,k) for a,p in zip(actual, predicted)])
def personalization(predicted: List[list]) -> float:
"""
Personalization measures recommendation similarity across users.
A high score indicates good personalization (user's lists of recommendations are different).
A low score indicates poor personalization (user's lists of recommendations are very similar).
A model is "personalizing" well if the set of recommendations for each user is different.
Parameters:
----------
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
Returns:
-------
The personalization score for all recommendations.
"""
def make_rec_matrix(predicted: List[list]) -> sp.csr_matrix:
df = pd.DataFrame(data=predicted).reset_index().melt(
id_vars='index', value_name='item',
)
df = df[['index', 'item']].pivot(index='index', columns='item', values='item')
df = pd.notna(df)*1
rec_matrix = sp.csr_matrix(df.values)
return rec_matrix
#create matrix for recommendations
predicted = np.array(predicted)
rec_matrix_sparse = make_rec_matrix(predicted)
#calculate similarity for every user's recommendation list
similarity = cosine_similarity(X=rec_matrix_sparse, dense_output=False)
#calculate average similarity
dim = similarity.shape[0]
personalization = (similarity.sum() - dim) / (dim * (dim - 1))
return 1-personalization
def _single_list_similarity(predicted: list, feature_df: pd.DataFrame, u: int) -> float:
"""
Computes the intra-list similarity for a single list of recommendations.
Parameters
----------
predicted : a list
Ordered predictions
Example: ['X', 'Y', 'Z']
feature_df: dataframe
A dataframe with one hot encoded or latent features.
The dataframe should be indexed by the id used in the recommendations.
Returns:
-------
ils_single_user: float
The intra-list similarity for a single list of recommendations.
"""
# exception predicted list empty
if not(predicted):
raise Exception('Predicted list is empty, index: {0}'.format(u))
#get features for all recommended items
recs_content = feature_df.loc[predicted]
recs_content = recs_content.dropna()
recs_content = sp.csr_matrix(recs_content.values)
#calculate similarity scores for all items in list
similarity = cosine_similarity(X=recs_content, dense_output=False)
#get indicies for upper right triangle w/o diagonal
upper_right = np.triu_indices(similarity.shape[0], k=1)
#calculate average similarity score of all recommended items in list
ils_single_user = np.mean(similarity[upper_right])
return ils_single_user
def intra_list_similarity(predicted: List[list], feature_df: pd.DataFrame) -> float:
"""
Computes the average intra-list similarity of all recommendations.
This metric can be used to measure diversity of the list of recommended items.
Parameters
----------
predicted : a list of lists
Ordered predictions
Example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
feature_df: dataframe
A dataframe with one hot encoded or latent features.
The dataframe should be indexed by the id used in the recommendations.
Returns:
-------
The average intra-list similarity for recommendations.
"""
feature_df = feature_df.fillna(0)
Users = range(len(predicted))
ils = [_single_list_similarity(predicted[u], feature_df, u) for u in Users]
return np.mean(ils)
def mse(y: list, yhat: np.array) -> float:
"""
Computes the mean square error (MSE)
Parameters
----------
yhat : Series or array. Reconstructed (predicted) ratings or interaction values.
y: original true ratings or interaction values.
Returns:
-------
The mean square error (MSE)
"""
mse = mean_squared_error(y, yhat)
return mse
def rmse(y: list, yhat: np.array) -> float:
"""
Computes the root mean square error (RMSE)
Parameters
----------
yhat : Series or array. Reconstructed (predicted) ratings or values
y: original true ratings or values.
Returns:
-------
The root mean square error (RMSE)
"""
rmse = sqrt(mean_squared_error(y, yhat))
return rmse
def make_confusion_matrix(y: list, yhat: list) -> None:
"""
Calculates and plots a confusion matrix
Parameters
----------
y : list or array of actual interaction values such as ratings
yhat: list or array of actual predicted interaction values
Returns:
-------
A confusion matrix plot
"""
cm = confusion_matrix(y, yhat, labels=[1,0])
cm = np.round(cm.astype('float') / cm.sum(axis=1)[:, np.newaxis],4)*100
fmt = ".2f"
_ = cm.max() / 2. # TODO: Unused argument
descriptions = np.array([["True Positive", "False Negative"], ["False Positive", "True Negatives"]])
colors = np.array([["green", "red"], ["red", "green"]])
plt.imshow([[0,0],[0,0]], interpolation='nearest', cmap=plt.cm.Greys)
for i, j in product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt)+'%\n' + descriptions[i, j],
horizontalalignment="center",
color=colors[i,j])
plt.axhline(y=0.5, xmin=0, xmax=1, color="black", linewidth=0.75)
plt.axvline(x=0.5, ymin=0, ymax=1, color="black", linewidth=0.75)
plt.ylabel('True')
plt.xlabel('Predicted')
plt.title("Confusion Matrix")
plt.xticks([0,1], [1,0], rotation=45)
plt.yticks([0,1], [1,0])
plt.show()
def _precision(predicted, actual):
prec = [value for value in predicted if value in actual]
prec = float(len(prec)) / float(len(predicted))
return prec
def recommender_precision(predicted: List[list], actual: List[list]) -> int:
"""
Computes the precision of each user's list of recommendations, and averages precision over all users.
----------
actual : a list of lists
Actual items to be predicted
example: [['A', 'B', 'X'], ['A', 'B', 'Y']]
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
Returns:
-------
precision: int
"""
precision = np.mean(list(map(lambda x, y: np.round(_precision(x,y), 4), predicted, actual)))
return precision
def recommender_recall(predicted: List[list], actual: List[list]) -> int:
"""
Computes the recall of each user's list of recommendations, and averages precision over all users.
----------
actual : a list of lists
Actual items to be predicted
example: [['A', 'B', 'X'], ['A', 'B', 'Y']]
predicted : a list of lists
Ordered predictions
example: [['X', 'Y', 'Z'], ['X', 'Y', 'Z']]
Returns:
-------
recall: int
"""
def calc_recall(predicted, actual):
reca = [value for value in predicted if value in actual]
reca = np.round(float(len(reca)) / float(len(actual)), 4)
return reca
recall = np.mean(list(map(calc_recall, predicted, actual)))
return recall