-
Notifications
You must be signed in to change notification settings - Fork 726
/
eval_utils.py
70 lines (57 loc) · 2.02 KB
/
eval_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from sklearn.metrics import roc_auc_score
import numpy as np
def dcg_score(y_true, y_score, k=10):
order = np.argsort(y_score)[::-1]
y_true = np.take(y_true, order[:k])
gains = 2**y_true - 1
discounts = np.log2(np.arange(len(y_true)) + 2)
return np.sum(gains / discounts)
def ndcg_score(y_true, y_score, k=10):
best = dcg_score(y_true, y_true, k)
actual = dcg_score(y_true, y_score, k)
return actual / best
def mrr_score(y_true, y_score):
order = np.argsort(y_score)[::-1]
y_true = np.take(y_true, order)
rr_score = y_true / (np.arange(len(y_true)) + 1)
return np.sum(rr_score) / np.sum(y_true)
def evaluate(predicted_label, label, bound):
predicted_label = predicted_label[:, 0]
AUC = []
MRR = []
nDCG5 = []
nDCG10 = []
for i in range(len(bound)):
start, ed = bound[i]
score = predicted_label[start:ed]
labels = label[start:ed]
auc = roc_auc_score(labels, score)
mrr = mrr_score(labels, score)
ndcg5 = ndcg_score(labels, score, k=5)
ndcg10 = ndcg_score(labels, score, k=10)
AUC.append(auc)
MRR.append(mrr)
nDCG5.append(ndcg5)
nDCG10.append(ndcg10)
AUC = np.array(AUC)
MRR = np.array(MRR)
nDCG5 = np.array(nDCG5)
nDCG10 = np.array(nDCG10)
AUC = AUC.mean()
MRR = MRR.mean()
nDCG5 = nDCG5.mean()
nDCG10 = nDCG10.mean()
return AUC, MRR, nDCG5, nDCG10