-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics_code.py
298 lines (241 loc) · 11.7 KB
/
metrics_code.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : metrics_code.py
@Version : 1.0
@Author : Forrest Stone
@Contact : [email protected]
@Time : 2023/02/04 11:06:27
@Desc : The implements of diversity metric in recommednation
'''
# here put the import lib
import numpy as np
from math import log
from copy import deepcopy
# calculate the ILAD of single user
def get_ILAD_per_user_at_k(predicted, item_representations, topk):
"""
The intra-list average distance, the average dis-similarity of every pair items in the list, here we adopt the (1 - cos similarity) to calculate the distance between two items
Args:
predicted (list): The list of predicted items of single user, [i1,i2,...]
item_representations (array): The representation of all items [number_item * dimension_item]
topk (int): The length of the list we want to calculate
Returns:
float: The value of ILAD
"""
local_depth = min(len(predicted), topk)
list_items = item_representations[predicted][:local_depth]
list_items = list_items / np.linalg.norm(
list_items, axis=-1, keepdims=True)
dis_matrix = np.dot(list_items, list_items.T)
dis_matrix = 1 - dis_matrix
dis_sum = np.sum(dis_matrix) / 2
ILAD = np.divide(dis_sum, dis_matrix.shape[0] * (dis_matrix.shape[0] - 1))
return ILAD
# calculate the ILAD of all users
def get_ILAD_at_k(predicted, item_representations, topk):
ILAD = 0.0
num_users = len(predicted)
for user_id in range(num_users):
ILAD += get_ILAD_per_user_at_k(predicted[user_id],
item_representations, topk)
return ILAD / num_users
# The item-level coverage, calculate signle user's coverage is useless since the recommednation list of every user is unchanged, always topk
def coverage_items_system_at_k(predicted, items, topk):
"""
The system-level coverage, the set of recommended items for all users divided by all items
Args:
predicted (list): The list of predicted items of all users, [[i1,i2,...], [i1,i2,...]]
items (list): The all items list
topk (int): The length of the list we want to calculate
Returns:
float: The value of coverage
"""
items_list = []
num_users = len(predicted)
for user_id in range(num_users):
local_depth = min(len(predicted[user_id]), topk)
item_list_per_user = predicted[user_id][:local_depth]
items_list.append(item_list_per_user)
item_set = set(sum(items_list, []))
coverage = len(item_set) / len(items)
return coverage
# The subtopic-level coverage, two aspects, this one is the user level, this is, calculate the coverage of a single user and average all users
def coverage_subtopics_user_at_k(predicted, topics, items_topics_dict, topk):
"""
The user-level coverage subtopic, first, calculate every user's subtopic in this recommendation list, and then divide by the total topics, finally, sum all users' results, and then average
Args:
predicted (list): The list of predicted items of all users, [[i1,i2,...], [i1,i2,...]]
topics (list): The topics list
items_topics_dict (dict): The items and topics dict, {item1:[topic1, topic2, ...], item2:[topic1, topic2,...]}
topk (int): The length of the list we want to calculate
Returns:
float: The value of user-level subtopic coverage
"""
coverage_items_user = 0.0
num_users = len(predicted)
for user_id in range(num_users):
topics_set_one_user = set()
local_depth = min(len(predicted[user_id]), topk)
item_list_per_user = predicted[user_id][:local_depth]
for item_id in range(local_depth):
topic_list_per_user = items_topics_dict[item_list_per_user[item_id]]
topics_set_one_user = topics_set_one_user | set(
topic_list_per_user)
topics_per_user = len(set(topics_set_one_user)) / topic_num
coverage_items_user += topics_per_user
return coverage_items_user / num_users
# The subtopic-level coverage, two aspects, this one is the system level, this is, calculate the coverage of all users directly
def coverage_subtopics_system_at_k(predicted, topics, items_topics_dict, topk):
"""
The system-level coverage subtopic, first, calculate every user's subtopic in this recommendation list, and then get all users topics, then divide by all topics
Args:
predicted (list): The list of predicted items of all users, [[i1,i2,...], [i1,i2,...]]
topics (list): The topics list
items_topics_dict (dict): The items and topics dict, {item1:[topic1, topic2, ...], item2:[topic1, topic2,...]}
topk (int): The length of the list we want to calculate
Returns:
float: The value of system-level subtopic coverage
"""
topics_list_all_user = []
topics_list_one_user = []
num_users = len(predicted)
for user_id in range(num_users):
local_depth = min(len(predicted[user_id]), topk)
item_list_per_user = predicted[user_id][:local_depth]
for item_id in range(local_depth):
topic_list_per_user = items_topics_dict[item_list_per_user[item_id]]
topics_list_one_user.append(topic_list_per_user)
topics_per_user = list(set(sum(topics_list_one_user, [])))
topics_list_all_user.append(topics_per_user)
topics_all_user_set = set(sum(topics_list_all_user, []))
coverage_items_system = len(topics_all_user_set) / len(topics)
return coverage_items_system
def get_ideal_ranking(predicted, user_topic_list, items_topics_dict, alpha, topk):
ideal_ranking = []
topics_user = set(user_topic_list)
topics_number_of_occurrences = dict(
zip(topics_user, np.zeros(len(topics_user))))
item_candidates = set(deepcopy(predicted))
while len(item_candidates) > 0 and len(ideal_ranking) < topk:
bestValue = float("-inf")
whoIsBest = "noOne"
topics_of_best = set()
topics_intersections = {}
for item in item_candidates:
topics_intersections[item] = deepcopy(
set(items_topics_dict[item]) & topics_user)
for item in item_candidates:
value = 0.0
for topic in topics_intersections[item]:
value += ((1 - alpha)**topics_number_of_occurrences[topic]) / log(
2 + len(ideal_ranking), 2)
if value > bestValue:
bestValue = deepcopy(value)
whoIsBest = deepcopy(item)
topics_of_best = deepcopy(topics_intersections[item])
for topic in topics_of_best:
topics_number_of_occurrences[topic] += 1
ideal_ranking.append(deepcopy(whoIsBest))
item_candidates.remove(whoIsBest)
return ideal_ranking
def get_alpha_DCG_per_user_at_k(ranking, user_topic_list, items_topics_dict, alpha, topk):
topics_user = set(user_topic_list)
topics_number_of_occurrences = dict(
zip(topics_user, np.zeros(len(topics_user))))
local_depth = min(topk, len(ranking))
local_dcg_values = np.zeros(local_depth)
value = 0.0
for i in range(0, local_depth):
topics_intersection = (
set(items_topics_dict[ranking[i]]) & topics_user)
for topic in topics_intersection:
value += ((1 - alpha) **
topics_number_of_occurrences[topic]) / log(2 + i, 2)
topics_number_of_occurrences[topic] += 1
local_dcg_values[i] = deepcopy(value)
return local_dcg_values
def alpha_nDCG_per_user_at_k(predicted, user_topic_list, items_topics_dict, alpha, topk):
"""
The alpha_nDCG for a singal user methods
Args:
predicted (list): The list of predicted items of single user, [i1,i2,...]
user_topic_list (list): The topics of one user, [topic1, topic2,...]
items_topics_dict (dict): The items and topics dict, {item1:[topic1, topic2, ...], item2:[topic1, topic2,...]}
alpha (float): The redundancy penalty
topk (int): The length of the list we want to calculate
Reference:
https://github.com/Pabfigueira/alpha-nDCG
Returns:
list: The alpha-nDCG of every position for a single user
"""
local_depth = min(len(predicted), topk)
ideal_ranking = get_ideal_ranking(
predicted, user_topic_list, items_topics_dict, alpha, topk)
dcg_target_ranking = get_alpha_DCG_per_user_at_k(
predicted, user_topic_list, items_topics_dict, alpha, local_depth)
dcg_ideal_ranking = get_alpha_DCG_per_user_at_k(
ideal_ranking, user_topic_list, items_topics_dict, alpha, local_depth)
ndcg_values = np.zeros(local_depth)
for i in range(0, local_depth):
if dcg_target_ranking[i] == 0.0:
ndcg_values[i] = 0.0
else:
ndcg_values[i] = deepcopy(
dcg_target_ranking[i] / dcg_ideal_ranking[i])
return ndcg_values
def get_alpha_DCG_all_user_at_k(predicted, user_topic_dict, items_topics_dict, alpha, topk):
num_users = len(predicted)
local_dcg_values = {}
for user_id in range(num_users):
topics_user = set(user_topic_dict[user_id])
topics_number_of_occurrences = dict(
zip(topics_user, np.zeros(len(topics_user))))
local_depth = min(topk, len(predicted[user_id]))
local_dcg_values[user_id] = np.zeros(local_depth)
value = 0.0
for i in range(0, local_depth):
topics_intersection = (
set(items_topics_dict[predicted[user_id][i]])
& topics_user)
for topic in topics_intersection:
value += (
(1 - alpha) **
topics_number_of_occurrences[topic]) / log(2 + i, 2)
topics_number_of_occurrences[topic] += 1
local_dcg_values[user_id][i] = deepcopy(value)
return local_dcg_values
def get_alpha_nDCG_all_user_at_k(predicted, user_topic_dict, items_topics_dict, alpha, topk):
"""
The alpha_nDCG for all users methods
Args:
predicted (list): The list of predicted items of all users, [[i1,i2,...], [i1,i2,...]]
user_topic_dict (dict): The users and topics dict, {user1:[topic1, topic2, ...], user2:[topic1, topic2,...]}
items_topics_dict (dict): The items and topics dict, {item1:[topic1, topic2, ...], item2:[topic1, topic2,...]}
alpha (float): The redundancy penalty
topk (int): The length of the list we want to calculate
Reference:
https://github.com/Pabfigueira/alpha-nDCG
Returns:
dict: The alpha-nDCG of every position for all users
"""
dcg_values = deepcopy(get_alpha_DCG_all_user_at_k(
predicted, user_topic_dict, items_topics_dict, alpha, topk))
ndcg_values = {}
num_users = len(predicted)
for user_id in range(num_users):
local_depth = min(topk, len(predicted[user_id]))
idealRanking = get_ideal_ranking(
predicted, user_topic_dict[user_id], items_topics_dict, alpha, topk)
auxiliarDict = []
auxiliarDict.append(deepcopy(idealRanking))
dcg_ideal_ranking = get_alpha_DCG_all_user_at_k(
auxiliarDict, user_topic_dict, items_topics_dict, alpha, topk)
ndcg_values[user_id] = np.zeros(local_depth)
for i in range(0, local_depth):
if dcg_values[user_id][i] == 0.0:
ndcg_values[user_id][i] = 0.0
else:
ndcg_values[user_id][i] = (dcg_values[user_id][i] /
dcg_ideal_ranking[user_id][i])
return ndcg_values