-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation.py
executable file
·151 lines (127 loc) · 5.34 KB
/
evaluation.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
import json
import numpy as np
from functools import partial
from scipy.sparse import csr_matrix
from sklearn.preprocessing import MultiLabelBinarizer
from typing import Union, Optional, List, Iterable, Hashable
import os
import argparse
import warnings
warnings.filterwarnings('ignore')
TPredict = np.ndarray
TTarget = Union[Iterable[Iterable[Hashable]], csr_matrix]
TMlb = Optional[MultiLabelBinarizer]
TClass = Optional[List[Hashable]]
def get_mlb(classes: TClass = None, mlb: TMlb = None, targets: TTarget = None):
if classes is not None:
mlb = MultiLabelBinarizer(classes, sparse_output=True)
if mlb is None and targets is not None:
if isinstance(targets, csr_matrix):
mlb = MultiLabelBinarizer(range(targets.shape[1]), sparse_output=True)
mlb.fit(None)
else:
mlb = MultiLabelBinarizer(sparse_output=True)
mlb.fit(targets)
return mlb
def get_precision(prediction: TPredict, targets: TTarget, mlb: TMlb = None, classes: TClass = None, top=5):
mlb = get_mlb(classes, mlb, targets)
if not isinstance(targets, csr_matrix):
targets = mlb.transform(targets)
prediction = mlb.transform(prediction[:, :top])
return prediction.multiply(targets).sum() / (top * targets.shape[0])
get_p_1 = partial(get_precision, top=1)
get_p_3 = partial(get_precision, top=3)
get_p_5 = partial(get_precision, top=5)
def get_ndcg(prediction: TPredict, targets: TTarget, mlb: TMlb = None, classes: TClass = None, top=5):
mlb = get_mlb(classes, mlb, targets)
log = 1.0 / np.log2(np.arange(top) + 2)
dcg = np.zeros((targets.shape[0], 1))
if not isinstance(targets, csr_matrix):
targets = mlb.transform(targets)
for i in range(top):
p = mlb.transform(prediction[:, i: i+1])
dcg += p.multiply(targets).sum(axis=-1) * log[i]
return np.average(dcg / log.cumsum()[np.minimum(targets.sum(axis=-1), top) - 1])
get_n_3 = partial(get_ndcg, top=3)
get_n_5 = partial(get_ndcg, top=5)
def get_inv_propensity(train_y: csr_matrix, a=0.55, b=1.5):
n, number = train_y.shape[0], np.asarray(train_y.sum(axis=0)).squeeze()
c = (np.log(n) - 1) * ((b + 1) ** a)
return 1.0 + c * (number + b) ** (-a)
def get_psp(prediction: TPredict, targets: TTarget, inv_w: np.ndarray, mlb: TMlb = None,
classes: TClass = None, top=5):
mlb = get_mlb(classes, mlb)
if not isinstance(targets, csr_matrix):
targets = mlb.transform(targets)
prediction = mlb.transform(prediction[:, :top]).multiply(inv_w)
num = prediction.multiply(targets).sum()
t, den = csr_matrix(targets.multiply(inv_w)), 0
for i in range(t.shape[0]):
den += np.sum(np.sort(t.getrow(i).data)[-top:])
return num / den
get_psp_1 = partial(get_psp, top=1)
get_psp_3 = partial(get_psp, top=3)
get_psp_5 = partial(get_psp, top=5)
def get_psndcg(prediction: TPredict, targets: TTarget, inv_w: np.ndarray, mlb: TMlb = None,
classes: TClass = None, top=5):
mlb = get_mlb(classes, mlb)
log = 1.0 / np.log2(np.arange(top) + 2)
psdcg = 0.0
if not isinstance(targets, csr_matrix):
targets = mlb.transform(targets)
for i in range(top):
p = mlb.transform(prediction[:, i: i+1]).multiply(inv_w)
psdcg += p.multiply(targets).sum() * log[i]
t, den = csr_matrix(targets.multiply(inv_w)), 0.0
for i in range(t.shape[0]):
num = min(top, len(t.getrow(i).data))
den += -np.sum(np.sort(-t.getrow(i).data)[:num] * log[:num])
return psdcg / den
get_psndcg_3 = partial(get_psndcg, top=3)
get_psndcg_5 = partial(get_psndcg, top=5)
parser = argparse.ArgumentParser(description='main', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dataset', required=True, type=str)
parser.add_argument('--model', required=True, type=str)
args = parser.parse_args()
dataset = args.dataset
model_name = args.model
preds = []
targets = []
with open(f'{dataset}/{dataset}_paper.json') as fin1, \
open(f'{dataset}/{dataset}_predictions_futex.json') as fin2:
for line1, line2 in zip(fin1, fin2):
data1 = json.loads(line1)
data2 = json.loads(line2)
targets.append(data1['label'])
pred = [x[0] for x in data2['predictions']]
pred = pred[:5] + ['PAD']*(5-len(pred))
preds.append(pred)
preds = np.array(preds)
mlb = MultiLabelBinarizer(sparse_output=True)
targets = mlb.fit_transform(targets)
p1, p3, p5, n3, n5 = get_p_1(preds, targets, mlb), get_p_3(preds, targets, mlb), get_p_5(preds, targets, mlb), \
get_n_3(preds, targets, mlb), get_n_5(preds, targets, mlb)
print('P@1:', p1, ', ', \
'P@3:', p3, ', ', \
'P@5:', p5, ', ', \
'NDCG@3:', n3, ', ', \
'NDCG@5:', n5)
with open('scores.txt', 'a') as fout:
fout.write('{:.4f}'.format(p1)+'\t'+'{:.4f}'.format(p3)+'\t'+'{:.4f}'.format(p5)+'\t'+ \
'{:.4f}'.format(n3)+'\t'+'{:.4f}'.format(n5)+'\n')
train_labels = []
with open(f'{dataset}/{dataset}_paper.json') as fin:
for line in fin:
data = json.loads(line)
train_labels.append(data['label'])
inv_w = get_inv_propensity(mlb.transform(train_labels), 0.55, 1.5)
psp1, psp3, psp5, psn3, psn5 = get_psp_1(preds, targets, inv_w, mlb), get_psp_3(preds, targets, inv_w, mlb), get_psp_5(preds, targets, inv_w, mlb), \
get_psndcg_3(preds, targets, inv_w, mlb), get_psndcg_5(preds, targets, inv_w, mlb)
print('PSP@1:', psp1, ', ', \
'PSP@3:', psp3, ', ', \
'PSP@5:', psp5, ', ', \
'PSN@3:', psn3, ', ', \
'PSN@5:', psn5)
with open('scores.txt', 'a') as fout:
fout.write('{:.4f}'.format(psp1)+'\t'+'{:.4f}'.format(psp3)+'\t'+'{:.4f}'.format(psp5)+'\t'+ \
'{:.4f}'.format(psn3)+'\t'+'{:.4f}'.format(psn5)+'\n')