-
Notifications
You must be signed in to change notification settings - Fork 0
/
HPSearch_sklearn_visualization.py
102 lines (81 loc) · 4.11 KB
/
HPSearch_sklearn_visualization.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
import pandas as pd
from hyperopt import plotting
import os.path, joblib, sys
from functions.sub_sklearn_hyperopt import map_and_clean_hparams
from global_vars import path_tables
def get_hparams_info(classifier: str, resampling = 'None'):
'''
Print the obtained choice of hyper-params for all profiles of a classifier.
'''
# initialize variable for storing individual hparams
hparam_all = {}
print('Classifier: ', classifier)
print('----------------------------')
for surrender_profile in range(4):
cwd = os.path.dirname(os.path.realpath(__file__))
path_save_models = os.path.join(cwd,r'profile_{}'.format(surrender_profile))
# adjust path is resampling is applied (optional)
if resampling == 'SMOTE':
path_save_models = os.path.join(os.path.join(path_save_models, r'models'), r'SMOTE')
name_tag = '_'+resampling
elif resampling == 'undersampling':
path_save_models = os.path.join(os.path.join(path_save_models, r'models'), r'Undersampling')
name_tag = '_' + resampling
elif resampling == 'None':
name_tag = ''
else:
raise ValueError('resampling type unknown!')
classifier_type = classifier
try:
trials = joblib.load(os.path.join(path_save_models, r'hyperopt_{}.pkl'.format(classifier_type)))
params = map_and_clean_hparams(trials, classifier_type, surrender_profile)
print('\t \t runtime: ', trials.best_trial['result']['eval_time'], '\n')
# store values
hparam_all[surrender_profile] = params
except:
print(f'No HPSearch-object for type {classifier} and surrender_profile {surrender_profile} available yet.')
df = pd.DataFrame(hparam_all).transpose()
with open(os.path.join(path_tables,r'hparams_{}{}.tex'.format(classifier_type, name_tag)),'w') as f:
f.write(df.to_latex())
def plot_hparams_info(classifier: str, resampling='None'):
'''
Plot for all profiles of a classifier how the hyper-params populate the search-space.
'''
print('Classifier: ', classifier)
print('----------------------------')
for surrender_profile in range(4):
cwd = os.path.dirname(os.path.realpath(__file__))
path_save_models = os.path.join(cwd,r'profile_{}'.format(surrender_profile))
# adjust path is resampling is applied (optional)
if resampling == 'SMOTE':
path_save_models = os.path.join(os.path.join(path_save_models, r'models'), r'SMOTE')
name_tag = '_'+resampling
elif resampling == 'undersampling':
path_save_models = os.path.join(os.path.join(path_save_models, r'models'), r'Undersampling')
name_tag = '_' + resampling
elif resampling == 'None':
name_tag = ''
else:
raise ValueError('resampling type unknown!')
classifier_type = classifier
try:
trials = joblib.load(os.path.join(path_save_models, r'hyperopt_{}.pkl'.format(classifier_type)))
plotting.main_plot_history(trials, title= f'Loss History ({classifier_type+name_tag})')
# plotting.main_plot_vars(trials)
except:
print(f'No HPSearch-object for type {classifier} and surrender_profile {surrender_profile} available yet.')
if __name__ == '__main__':
# choose resampling type: cmd-line input argument
try:
res_type = sys.argv[1] # automated user input to be run with python 'filename' profile_nr
if res_type not in ['None', 'SMOTE', 'undersampling']:
raise ValueError('Unknown type of resampling. User input not compatible.')
except:
res_type = 'None'
# look up hparams and respective CV-loss values
for clf in ['boost_ann', 'ann', 'logit', 'rf', 'xgboost']:
get_hparams_info(classifier= clf, resampling=res_type)
print('Displaying hparam search process and loss')
# check how hparams populate feature space and stability of loss-values
for clf in ['boost_ann', 'ann', 'logit', 'rf', 'xgboost']:
plot_hparams_info(classifier= clf, resampling=res_type)