forked from THUIR/TranSIV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_new.py
executable file
·341 lines (281 loc) · 12.9 KB
/
rec_new.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
import bottleneck as bn
import numpy as np
from scipy import sparse
"""
All the data should be in the shape of (n_users, n_items)
All the latent factors should in the shape of (n_users/n_items, n_components)
1. train_data refers to the data that was used to train the model
2. heldout_data refers to the data that was used for evaluation (could be test
set or validation set)
3. vad_data refers to the data that should be excluded as validation set, which
should only be used when calculating test scores
"""
def prec_at_k(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000, k=20,
mu=None, vad_data=None, agg=np.nanmean):
n_users = train_data.shape[0]
res = list()
for user_idx in user_idx_generator(n_users, batch_users):
res.append(precision_at_k_batch(train_data, heldout_data,
U1, V1.T,U2,V2.T, user_idx, k=k,
mu=mu, vad_data=vad_data))
mn_prec = np.hstack(res)
if callable(agg):
return agg(mn_prec)
return mn_prec
def recall_at_k(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000, k=20,
mu=None, vad_data=None, agg=np.nanmean):
n_users = train_data.shape[0]
res = list()
for user_idx in user_idx_generator(n_users, batch_users):
res.append(recall_at_k_batch(train_data, heldout_data,
U1, V1.T,U2,V2.T, user_idx, k=k,
mu=mu, vad_data=vad_data))
mn_recall = np.hstack(res)
if callable(agg):
return agg(mn_recall)
return mn_recall
def ric_rank_at_k(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000, k=5,
mu=None, vad_data=None):
n_users = train_data.shape[0]
res = list()
for user_idx in user_idx_generator(n_users, batch_users):
res.append(mean_rrank_at_k_batch(train_data, heldout_data,
U1, V1.T,U2,V2, user_idx, k=k,
mu=mu, vad_data=vad_data))
mrrank = np.hstack(res)
return mrrank[mrrank > 0].mean()
def mean_perc_rank(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000,
mu=None, vad_data=None):
n_users = train_data.shape[0]
mpr = 0
for user_idx in user_idx_generator(n_users, batch_users):
mpr += mean_perc_rank_batch(train_data, heldout_data, U1, V1.T,U2,V2.T, user_idx,
mu=mu, vad_data=vad_data)
mpr /= heldout_data.sum()
return mpr
def normalized_dcg(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000,
mu=None, vad_data=None, agg=np.nanmean):
n_users = train_data.shape[0]
res = list()
for user_idx in user_idx_generator(n_users, batch_users):
res.append(NDCG_binary_batch(train_data, heldout_data, U1, V1.T,U2,V2.T,
user_idx, mu=mu, vad_data=vad_data))
ndcg = np.hstack(res)
if callable(agg):
return agg(ndcg)
return ndcg
def normalized_dcg_at_k(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000,
k=100, mu=None, vad_data=None, agg=np.nanmean):
n_users = train_data.shape[0]
res = list()
for user_idx in user_idx_generator(n_users, batch_users):
res.append(NDCG_binary_at_k_batch(train_data, heldout_data, U1,V1.T,U2, V2.T,
user_idx, k=k, mu=mu,
vad_data=vad_data))
ndcg = np.hstack(res)
if callable(agg):
return agg(ndcg)
return ndcg
def map_at_k(train_data, heldout_data, U1, V1,U2,V2, batch_users=5000, k=100, mu=None,
vad_data=None, agg=np.nanmean):
n_users = train_data.shape[0]
res = list()
for user_idx in user_idx_generator(n_users, batch_users):
res.append(MAP_at_k_batch(train_data, heldout_data, U1, V1.T,U2,V2.T, user_idx,
k=k, mu=mu, vad_data=vad_data))
map = np.hstack(res)
if callable(agg):
return agg(map)
return map
# helper functions #
def user_idx_generator(n_users, batch_users):
''' helper function to generate the user index to loop through the dataset
'''
for start in xrange(0, n_users, batch_users):
end = min(n_users, start + batch_users)
yield slice(start, end)
def _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx, batch_users, mu=None,
vad_data=None):
n_songs = train_data.shape[1]
# exclude examples from training and validation (if any)
item_idx = np.zeros((batch_users, n_songs), dtype=bool)
item_idx[train_data[user_idx].nonzero()] = True
if vad_data is not None:
item_idx[vad_data[user_idx].nonzero()] = True
X_pred = Et1[user_idx].dot(Eb1)+Et2[user_idx].dot(Eb2)
if mu is not None:
if isinstance(mu, np.ndarray):
assert mu.size == n_songs # mu_i
X_pred *= mu
elif isinstance(mu, dict): # func(mu_ui)
params, func = mu['params'], mu['func']
args = [params[0][user_idx], params[1]]
if len(params) > 2: # for bias term in document or length-scale
args += [params[2][user_idx]]
if not callable(func):
raise TypeError("expecting a callable function")
X_pred *= func(*args)
else:
raise ValueError("unsupported mu type")
X_pred[item_idx] = -np.inf
return X_pred
def precision_at_k_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2, user_idx,
k=20, normalize=False, mu=None, vad_data=None):
batch_users = user_idx.stop - user_idx.start
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx,
batch_users, mu=mu, vad_data=vad_data)
idx = bn.argpartsort(-X_pred, k, axis=1)
X_pred_binary = np.zeros_like(X_pred, dtype=bool)
X_pred_binary[np.arange(batch_users)[:, np.newaxis], idx[:, :k]] = True
X_true_binary = (heldout_data[user_idx] > 0).toarray()
tmp = (np.logical_and(X_true_binary, X_pred_binary).sum(axis=1)).astype(
np.float32)
if normalize:
precision = tmp / np.minimum(k, X_true_binary.sum(axis=1))
else:
precision = tmp / k
return precision
def recall_at_k_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2, user_idx,
k=20, normalize=True, mu=None, vad_data=None):
batch_users = user_idx.stop - user_idx.start
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx,
batch_users, mu=mu, vad_data=vad_data)
idx = bn.argpartsort(-X_pred, k, axis=1)
X_pred_binary = np.zeros_like(X_pred, dtype=bool)
X_pred_binary[np.arange(batch_users)[:, np.newaxis], idx[:, :k]] = True
X_true_binary = (heldout_data[user_idx] > 0).toarray()
tmp = (np.logical_and(X_true_binary, X_pred_binary).sum(axis=1)).astype(
np.float32)
recall = tmp / np.minimum(k, X_true_binary.sum(axis=1))
return recall
def mean_rrank_at_k_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2,
user_idx, k=5, mu=None, vad_data=None):
'''
mean reciprocal rank@k: For each user, make predictions and rank for
all the items. Then calculate the mean reciprocal rank for the top K that
are in the held-out set.
'''
batch_users = user_idx.stop - user_idx.start
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx,
batch_users, mu=mu, vad_data=vad_data)
all_rrank = 1. / (np.argsort(np.argsort(-X_pred, axis=1), axis=1) + 1)
X_true_binary = (heldout_data[user_idx] > 0).toarray()
heldout_rrank = X_true_binary * all_rrank
top_k = bn.partsort(-heldout_rrank, k, axis=1)
return -top_k[:, :k].mean(axis=1)
def NDCG_binary_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2, user_idx,
mu=None, vad_data=None):
'''
normalized discounted cumulative gain for binary relevance
'''
batch_users = user_idx.stop - user_idx.start
n_items = train_data.shape[1]
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx,
batch_users, mu=mu, vad_data=vad_data)
all_rank = np.argsort(np.argsort(-X_pred, axis=1), axis=1)
# build the discount template
tp = 1. / np.log2(np.arange(2, n_items + 2))
all_disc = tp[all_rank]
X_true_binary = (heldout_data[user_idx] > 0).tocoo()
disc = sparse.csr_matrix((all_disc[X_true_binary.row, X_true_binary.col],
(X_true_binary.row, X_true_binary.col)),
shape=all_disc.shape)
DCG = np.array(disc.sum(axis=1)).ravel()
IDCG = np.array([tp[:n].sum()
for n in heldout_data[user_idx].getnnz(axis=1)])
return DCG / IDCG
def NDCG_binary_at_k_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2, user_idx,
mu=None, k=100, vad_data=None):
'''
normalized discounted cumulative gain@k for binary relevance
ASSUMPTIONS: all the 0's in heldout_data indicate 0 relevance
'''
batch_users = user_idx.stop - user_idx.start
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx,
batch_users, mu=mu, vad_data=vad_data)
idx_topk_part = bn.argpartsort(-X_pred, k, axis=1)
topk_part = X_pred[np.arange(batch_users)[:, np.newaxis],
idx_topk_part[:, :k]]
idx_part = np.argsort(-topk_part, axis=1)
# X_pred[np.arange(batch_users)[:, np.newaxis], idx_topk] is the sorted
# topk predicted score
idx_topk = idx_topk_part[np.arange(batch_users)[:, np.newaxis], idx_part]
# build the discount template
tp = 1. / np.log2(np.arange(2, k + 2))
heldout_batch = heldout_data[user_idx]
DCG = (heldout_batch[np.arange(batch_users)[:, np.newaxis],
idx_topk].toarray() * tp).sum(axis=1)
IDCG = np.array([(tp[:min(n, k)]).sum()
for n in heldout_batch.getnnz(axis=1)])
return DCG / IDCG
def MAP_at_k_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2, user_idx, mu=None, k=100,
vad_data=None):
'''
mean average precision@k
'''
batch_users = user_idx.stop - user_idx.start
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx, batch_users, mu=mu,
vad_data=vad_data)
idx_topk_part = bn.argpartsort(-X_pred, k, axis=1)
topk_part = X_pred[np.arange(batch_users)[:, np.newaxis],
idx_topk_part[:, :k]]
idx_part = np.argsort(-topk_part, axis=1)
# X_pred[np.arange(batch_users)[:, np.newaxis], idx_topk] is the sorted
# topk predicted score
idx_topk = idx_topk_part[np.arange(batch_users)[:, np.newaxis], idx_part]
aps = np.zeros(batch_users)
for i, idx in enumerate(xrange(user_idx.start, user_idx.stop)):
actual = heldout_data[idx].nonzero()[1]
if len(actual) > 0:
predicted = idx_topk[i]
aps[i] = apk(actual, predicted, k=k)
else:
aps[i] = np.nan
return aps
def mean_perc_rank_batch(train_data, heldout_data, Et1, Eb1,Et2,Eb2, user_idx,
mu=None, vad_data=None):
'''
mean percentile rank for a batch of users
MPR of the full set is the sum of batch MPR's divided by the sum of all the
feedbacks. (Eq. 8 in Hu et al.)
This metric not necessarily constrains the data to be binary
'''
batch_users = user_idx.stop - user_idx.start
X_pred = _make_prediction(train_data, Et1, Eb1,Et2,Eb2, user_idx, batch_users,
mu=mu, vad_data=vad_data)
all_perc = np.argsort(np.argsort(-X_pred, axis=1), axis=1) / \
np.isfinite(X_pred).sum(axis=1, keepdims=True).astype(np.float32)
perc_batch = (all_perc[heldout_data[user_idx].nonzero()] *
heldout_data[user_idx].data).sum()
return perc_batch
## from https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py
def apk(actual, predicted, k=100):
"""
Computes the average precision at k.
This function computes the average prescision at k between two lists of
items.
Parameters
----------
actual : list
A list of elements that are to be predicted (order doesn't matter)
predicted : list
A list of predicted elements (order does matter)
k : int, optional
The maximum number of predicted elements
Returns
-------
score : double
The average precision at k over the input lists
"""
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]: # not necessary for us since we will not make duplicated recs
num_hits += 1.0
score += num_hits / (i+1.0)
# we handle this part before making the function call
#if not actual:
# return np.nan
return score / min(len(actual), k)