-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_utils.py
46 lines (40 loc) · 2.01 KB
/
run_utils.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
import numpy as np
def ndcg(top_k_dict, k_options, test, user_column, item_column):
discounted_gain_per_k = np.array([1 / np.log2(i + 1) for i in range(1, max(k_options) + 1)])
ideal_discounted_gain_per_k = [discounted_gain_per_k[:ind + 1].sum() for ind, k in enumerate(discounted_gain_per_k)]
ndcg_per_user_per_k = {}
for k in k_options:
ndcg_per_user_per_k[k] = []
for user, predictions in top_k_dict.items():
positive_test_interactions = test[item_column][test[user_column] == user].values
hits = np.in1d(predictions[:max(k_options)], positive_test_interactions)
user_dcg = np.where(hits, discounted_gain_per_k[:len(hits)], 0)
for k in k_options:
user_ndcg = user_dcg[:k].sum() / ideal_discounted_gain_per_k[k - 1]
ndcg_per_user_per_k[k].append(user_ndcg)
return ndcg_per_user_per_k
def hr(top_k_dict, k_options, test, user_column, item_column):
hr_per_user_per_k = {}
for k in k_options:
hr_per_user_per_k[k] = []
for user, predictions in top_k_dict.items():
positive_test_interactions = test[item_column][test[user_column] == user].values
hits = np.in1d(predictions[:max(k_options)], positive_test_interactions)
for k in k_options:
user_hr = hits[:k].sum()
user_hr = 1 if user_hr > 0 else 0
hr_per_user_per_k[k].append(user_hr)
return hr_per_user_per_k
def recall(top_k_dict, k_options, test, user_column, item_column):
recall_per_user_per_k = {}
for k in k_options:
recall_per_user_per_k[k] = []
for user, predictions in top_k_dict.items():
positive_test_interactions = test[item_column][test[user_column] == user].values
hits = np.in1d(predictions[:max(k_options)], positive_test_interactions)
for k in k_options:
if user == 8:
pass
user_recall = hits[:k].sum() / min(len(positive_test_interactions), k)
recall_per_user_per_k[k].append(user_recall)
return recall_per_user_per_k