-
Notifications
You must be signed in to change notification settings - Fork 1
/
cross_validation_evaluation.py
85 lines (72 loc) · 3.27 KB
/
cross_validation_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
import os
from os import path
import argparse
import json
import logging
from sklearn import metrics
logging.getLogger().setLevel(logging.INFO)
if __name__ == "__main__":
"""##### Parameter parsing"""
parser = argparse.ArgumentParser(description="Cross Validation evaluation producer for EVALITA2018 ITAmoji task")
parser.add_argument("--input-file",
default=None,
required=True,
help="Input file path")
parser.add_argument("--train-file",
default=None,
required=True,
help="EVALITA train file")
args = parser.parse_args()
input_file_path = args.input_file
train_file_path = args.train_file
label_dictionary = {}
samples = {}
duplicates = 0
with open(train_file_path, "r", encoding="utf-8") as train_file:
for line in train_file:
sample = json.loads(line.rstrip())
if sample["tid"] in samples:
duplicates += 1
samples[sample["tid"]] = sample["label"]
if sample["label"] not in label_dictionary:
label_dictionary[sample["label"]] = len(label_dictionary)
logging.info("Loaded %d gold standard tweets (%d duplicates)" % (len(samples), duplicates))
folds = sorted(os.listdir(input_file_path))
for fold in folds:
if not fold.startswith("fold"):
continue
fold_dir = path.join(input_file_path, fold)
# Loading test set predictions
predictions = []
gold = []
with open(path.join(fold_dir, "fake_average_predictions.json"), "r", encoding="utf-8") as reader:
for line in reader:
sample = json.loads(line.rstrip())
if sample["tid"] not in samples:
logging.warning("[%s] Tweet %s was not found in gold standard" % (fold, sample["tid"]))
continue
predictions.append(sample["label_1"])
gold.append(samples[sample["tid"]])
logging.info("[%s] Loaded %d predictions" % (fold, len(predictions)))
accuracy_score = metrics.accuracy_score(gold, predictions)
precision_score = metrics.precision_score(gold, predictions, average="macro")
recall_score = metrics.recall_score(gold, predictions, average="macro")
f1_score = metrics.f1_score(gold, predictions, average="macro")
logging.info("[%s] Accuracy: %.4f, Prec: %.4f, Rec: %.4f, F1: %.4f" % (
fold,
accuracy_score,
precision_score,
recall_score,
f1_score
))
for file in ["real_predictions.json", "real_average_predictions.json"]:
dist = {}
lines = 0
with open(path.join(fold_dir, file), "r", encoding="utf-8") as reader:
for line in reader:
sample = json.loads(line.rstrip())
if sample["label_1"] not in dist:
dist[sample["label_1"]] = 0
dist[sample["label_1"]] += 1
lines += 1
print("["+file+"] Distribution: "+"\t".join([tuple[0]+":"+str(float(tuple[1])/lines) for tuple in sorted([(label, dist[label]) for label in dist], key= lambda x : -x[1])[:3]]))