-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtune_table.py
109 lines (96 loc) · 2.96 KB
/
tune_table.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
#!/usr/bin/env python3
"""Script to create table with tuning results for flat and hierarchical approaches."""
import argparse
import glob
import pickle
import sys
from argparse import Namespace
from typing import Tuple, List
import numpy as np
def parse_args(args: list) -> Namespace:
"""
Parse a list of arguments.
Parameters
----------
args : list
Arguments to parse.
Returns
-------
_ : Namespace
Parsed arguments.
"""
parser = argparse.ArgumentParser(
description="Create table with hyper-parameter tuning results"
)
parser.add_argument(
"--folder",
type=str,
required=True,
help="Folder where the tuning results are stored",
)
parser.add_argument(
"--model",
type=str,
required=True,
help="Model used for tuning",
)
parser.add_argument(
"--classifier",
type=str,
required=True,
help="Classifier used for tuning",
)
parser.add_argument(
"--output",
type=str,
required=True,
help="Output to write the table in markdown format (.md)",
)
return parser.parse_args(args)
def compute(
folder: str,
) -> Tuple[List[dict], List[list], List[np.ndarray], List[np.ndarray]]:
"""
Compute average and standard deviation of the tuning results.
Parameters
----------
folder : str
Folder where the tuning results are stored.
Returns
-------
hyperparameters : List[dict]
Hyperparameters tested for tuning.
scores : List[list]
Scores for each hyperparameter combination tested.
avg : List[np.ndarray]
Averages of k-fold cross-validation.
std : List[np.ndarray]
Standard deviations of k-fold cross-validation.
"""
results = glob.glob(f"{folder}/*.sav")
if "{}/trained_model.sav".format(folder) in results:
results.remove(f"{folder}/trained_model.sav")
hyperparameters = []
scores = []
avg = []
std = []
for result in results:
parameters, s = pickle.load(open(result, "rb"))
hyperparameters.append(parameters)
scores.append([round(i, 3) for i in s])
avg.append(np.mean(s))
std.append(np.std(s))
return hyperparameters, scores, avg, std
def create_table(args):
"""Create table with tuning results for flat and hierarchical approaches."""
with open(args.output, "w") as fout:
fout.write(f"# Model: {args.model}\n")
fout.write(f"## Base classifier: {args.classifier}\n")
fout.write("|Parameters|Scores|Average|Standard deviation|\n")
fout.write("|----------|------|-------|------------------|\n")
hyperparameters, scores, avg, std = compute(args.folder)
for hp, sc, av, st in zip(hyperparameters, scores, avg, std):
fout.write(f"|{hp}|{sc}|{av:.3f}|{st:.3f}|\n")
if __name__ == "__main__": # pragma: no cover
args = parse_args(sys.argv[1:])
create_table(args)