-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
executable file
·99 lines (80 loc) · 3.29 KB
/
eval.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
#!/usr/bin/python3
'''evaluation program for the clickbait challenge 2017.
web:
http://www.clickbait-challenge.org/
authors:
'''
import json
import sys
import sklearn.metrics as skm
import numpy as np
UNDERLINE = '\033[4m'
END = '\033[0m'
def usage():
usage = ''' Usage:
~$ python eval.py "truth.jsonl" "predictions.jsonl"
'''
print(usage)
exit()
def write_result(key, value):
value = round(value, ndigits=3)##Added by phil
print(key + ': ' + str(value))
def normalized_mean_squared_error(truth, predictions):
norm = skm.mean_squared_error(truth, np.full(len(truth), np.mean(truth)))
return skm.mean_squared_error(truth, predictions) / norm
regression_measures = {'Explained variance': skm.explained_variance_score,
'Mean absolute error': skm.mean_absolute_error,
'Mean squared error': skm.mean_squared_error,
'Median absolute error': skm.median_absolute_error,
'R2 score': skm.r2_score,
'Normalized mean squared error': normalized_mean_squared_error}
classification_measures = {'Accuracy': skm.accuracy_score,
'Precision': skm.precision_score,
'Recall': skm.recall_score,
'F1 score': skm.f1_score}
if __name__ == "__main__":
print("Start")
try:
with open(sys.argv[1], "r") as truth_file:
truth_dict = {json.loads(s)['id']: json.loads(s)['truthMean']
for s in truth_file.readlines()}
with open(sys.argv[1], "r") as truth_file:
class_dict = {json.loads(s)['id']: json.loads(s)['truthClass']
for s in truth_file.readlines()}
with open(sys.argv[2], "r") as preditcions_file:
predictions_dict = {json.loads(s)['id']: json.loads(s)['clickbaitScore']
for s in preditcions_file.readlines()}
except (KeyError, IndexError):
usage()
try:
truth = []
classes = []
predictions = []
for key in truth_dict:
truth.append(truth_dict[key])
classes.append(class_dict[key])
predictions.append(predictions_dict[key])
except KeyError:
print('missing id in predictions.')
exit()
print(UNDERLINE + '\nDataset Stats' + END)
write_result('Size', len(truth))
sum_clickbait = sum(1 for x in classes if x == 'clickbait')
write_result('#Clickbait', sum_clickbait)
write_result('#No-Clickbait', len(truth) - sum_clickbait)
print(UNDERLINE + '\nRegression scores' + END)
for name in sorted(regression_measures):
write_result(name,
regression_measures[name](truth, predictions)
)
print(UNDERLINE + '\nBinary classification scores' + END)
classes = [0 if t == 'no-clickbait' else 1 for t in classes]
predictions = [0 if t < 0.5 else 1 for t in predictions]
for name in sorted(classification_measures):
write_result(name,
classification_measures[name](classes, predictions)
)
# print(UNDERLINE + '\nClassification report' + END)
# print(skm.classification_report(classes, predictions))