-
Notifications
You must be signed in to change notification settings - Fork 1
/
myMetric.py
130 lines (112 loc) · 3.9 KB
/
myMetric.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
import numpy as np
def precision_k(true_mat, score_mat, k):
p = np.zeros((k, 1))
rank_mat = np.argsort(score_mat)
backup = np.copy(score_mat)
for k in range(k):
score_mat = np.copy(backup)
for i in range(rank_mat.shape[0]):
score_mat[i][rank_mat[i, :-(k + 1)]] = 0
score_mat = np.ceil(score_mat)
# kk = np.argwhere(score_mat>0)
mat = np.multiply(score_mat, true_mat)
# print("mat",mat)
num = np.sum(mat, axis=1)
p[k] = np.mean(num / (k + 1))
return np.around(p, decimals=4)
def recall_k(true_mat, score_mat, k):
p = np.zeros((k, 1))
rank_mat = np.argsort(score_mat)
backup = np.copy(score_mat)
for k in range(k):
score_mat = np.copy(backup)
for i in range(rank_mat.shape[0]):
score_mat[i][rank_mat[i, :-(k + 1)]] = 0
score_mat = np.ceil(score_mat)
# kk = np.argwhere(score_mat>0)
mat = np.multiply(score_mat, true_mat)
# print("mat",mat)
num = np.sum(mat, axis=1)
real_tag_num = np.array([sum(inst) for inst in true_mat])
p[k] = np.mean(num / real_tag_num)
return np.around(p, decimals=4)
def f1_score_k(true_mat, score_mat, k):
p = np.zeros((k, 1))
rank_mat = np.argsort(score_mat)
backup = np.copy(score_mat)
for k in range(k):
score_mat = np.copy(backup)
for i in range(rank_mat.shape[0]):
score_mat[i][rank_mat[i, :-(k + 1)]] = 0
score_mat = np.ceil(score_mat)
# kk = np.argwhere(score_mat>0)
mat = np.multiply(score_mat, true_mat)
# print("mat",mat)
num = np.sum(mat, axis=1)
pre = num / (k + 1)
real_tag_num = np.array([sum(inst) for inst in true_mat])
rec = num / real_tag_num
p[k] = np.mean(f1_score_compute(pre, rec))
return np.around(p, decimals=4)
def Ndcg_k(true_mat, score_mat, k):
res = np.zeros((k, 1))
rank_mat = np.argsort(score_mat)
backup = np.copy(score_mat)
label_count = np.sum(true_mat, axis=1)
for m in range(k):
y_mat = np.copy(true_mat)
for i in range(rank_mat.shape[0]):
y_mat[i][rank_mat[i, :-(m + 1)]] = 0
for j in range(m + 1):
y_mat[i][rank_mat[i, -(j + 1)]] /= np.log(j + 1 + 1)
dcg = np.sum(y_mat, axis=1)
factor = get_factor(label_count, m + 1)
ndcg = np.mean(dcg / factor)
res[m] = ndcg
return np.around(res, decimals=4)
def get_factor(label_count,k):
res=[]
for i in range(len(label_count)):
n=int(min(label_count[i],k))
f=0.0
for j in range(1,n+1):
f+=1/np.log(j+1)
res.append(f)
return np.array(res)
def f1_score_compute(pre, rec):
f1_score_lsit = []
for i in range(pre.shape[0]):
pre_i = pre[i]
re_i = rec[i]
if pre_i != 0 or re_i != 0:
f1_score_lsit.append(2 * pre_i * re_i / (pre_i + re_i))
else:
f1_score_lsit.append(0)
return np.array(f1_score_lsit)
def precision_kk(pred, label, k=[1, 3, 5]):
batch_size = pred.shape[0]
precision = []
for _k in k:
p = 0
for i in range(batch_size):
p += label[i, pred[i, :_k]].mean()
precision.append(p * 100 / batch_size)
return precision
def ndcg_kk(pred, label, k=[1, 3, 5]):
batch_size = pred.shape[0]
ndcg = []
for _k in k:
score = 0
rank = np.log2(np.arange(2, 2 + _k))
for i in range(batch_size):
l = label[i, pred[i, :_k]]
n = l.sum()
if (n == 0):
continue
dcg = (l / rank).sum()
label_count = label[i].sum()
norm = 1 / np.log2(np.arange(2, 2 + np.min((_k, label_count))))
norm = norm.sum()
score += dcg / norm
ndcg.append(score * 100 / batch_size)
return ndcg