-
Notifications
You must be signed in to change notification settings - Fork 26
/
selector.py
284 lines (232 loc) · 10 KB
/
selector.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# stdlib
from typing import Any, Dict, List, Tuple, Type, Union
# third party
from optuna.trial import Trial
# autoprognosis absolute
from autoprognosis.explorers.core.defaults import (
default_feature_scaling_names,
default_feature_selection_names,
)
import autoprognosis.logger as log
from autoprognosis.plugins.core.base_plugin import Plugin
import autoprognosis.plugins.core.params as params
from autoprognosis.plugins.imputers import Imputers
from autoprognosis.plugins.pipeline import Pipeline, PipelineMeta
from autoprognosis.plugins.prediction import Predictions
from autoprognosis.plugins.preprocessors import Preprocessors
predefined_args = {
"features_count": 10,
}
class PipelineSelector:
"""AutoML wrapper for pipelines
Args:
classifier: str
Last estimator of the pipeline, the final classifier.
calibration: int
Type of calibration to use. 0 - none, 1 - sigmoid, 2 - isotonic.
imputers: list
list of imputers to sample from.
feature_scaling: list
list of feature scaling transformers to sample from.
feature_selection: list
list of feature selection methods ti sample from
classifier_category: str
task type: "classifier" or "risk_estimation"
"""
def __init__(
self,
classifier: str,
calibration: List[int] = [0, 1, 2],
imputers: List[str] = [],
feature_scaling: List[str] = default_feature_scaling_names,
feature_selection: List[str] = default_feature_selection_names,
classifier_category: str = "classifier", # "classifier", "risk_estimation", "regression"
) -> None:
self.calibration = calibration
self.imputers = [Imputers().get_type(plugin) for plugin in imputers]
self.feature_scaling = [
Preprocessors(category="feature_scaling").get_type(plugin)
for plugin in feature_scaling
]
self.feature_selection = [
Preprocessors(category="dimensionality_reduction").get_type(plugin)
for plugin in feature_selection
]
if classifier == "multinomial_naive_bayes" or classifier == "bagging":
self.feature_scaling = [
Preprocessors(category="feature_scaling").get_type("minmax_scaler")
]
self.feature_selection = []
self.classifier = Predictions(category=classifier_category).get_type(classifier)
def _generate_dist_name(self, key: str) -> str:
if key == "imputation_candidate":
imputers_str = [imputer.name() for imputer in self.imputers]
imputers_str.sort()
return f"{self.classifier.fqdn()}.imputation_candidate.{'_'.join(imputers_str)}"
elif key == "feature_scaling_candidate":
fs_str = [fs.name() for fs in self.feature_scaling]
fs_str.sort()
return (
f"{self.classifier.fqdn()}.feature_scaling_candidate.{'_'.join(fs_str)}"
)
elif key == "feature_selection_candidate":
fs_str = [fs.name() for fs in self.feature_selection]
fs_str.sort()
return f"{self.classifier.fqdn()}.feature_selection_candidate.{'_'.join(fs_str)}"
else:
raise ValueError(f"invalid key {key}")
def hyperparameter_space(self) -> List:
hp: List[Union[params.Integer, params.Categorical, params.Float]] = []
if len(self.imputers) > 0:
hp.append(
params.Categorical(
self._generate_dist_name("imputation_candidate"),
[imputer.name() for imputer in self.imputers],
)
)
for plugin in self.imputers:
hp.extend(plugin.hyperparameter_space_fqdn(**predefined_args))
if len(self.feature_scaling) > 0:
hp.append(
params.Categorical(
self._generate_dist_name("feature_scaling_candidate"),
[fs.name() for fs in self.feature_scaling],
)
)
if len(self.feature_selection) > 0:
hp.append(
params.Categorical(
self._generate_dist_name("feature_selection_candidate"),
[fs.name() for fs in self.feature_selection],
)
)
for plugin in self.feature_selection:
hp.extend(plugin.hyperparameter_space_fqdn(**predefined_args))
if len(self.calibration) > 0:
hp.append(
params.Integer(
self.classifier.fqdn() + ".calibration",
0,
len(self.calibration) - 1,
),
)
hp.extend(self.classifier.hyperparameter_space_fqdn())
return hp
def sample_hyperparameters(self, trial: Trial) -> Dict:
params = self.hyperparameter_space()
result = {}
for param in params:
result[param.name] = param.sample(trial)
return result
def sample_hyperparameters_np(self) -> Dict:
params = self.hyperparameter_space()
result = {}
for param in params:
result[param.name] = param.sample_np()
return result
def name(self) -> str:
return self.classifier.name()
def get_pipeline_template(
self, search_domains: List[params.Params], hyperparams: List
) -> Tuple[List, Dict]:
domain_list = [search_domains[k].name for k in range(len(search_domains))]
model_list = list()
calibration = hyperparams[
domain_list.index(self.classifier.fqdn() + ".calibration")
]
args: Dict[str, Dict] = {
self.classifier.name(): {
"calibration": int(calibration),
},
}
def add_stage_hp(plugin: Type[Plugin]) -> None:
if plugin.name() not in args:
args[plugin.name()] = {}
for param in plugin.hyperparameter_space_fqdn(**predefined_args):
param_val = hyperparams[domain_list.index(param.name)]
param_val = type(param.bounds[0])(param_val)
args[plugin.name()][param.name.split(".")[-1]] = param_val
if len(self.imputers) > 0:
select_imp = hyperparams[
domain_list.index(self._generate_dist_name("imputation_candidate"))
]
selected = Imputers().get_type(select_imp)
model_list.append(selected.fqdn())
add_stage_hp(selected)
if len(self.feature_scaling) > 0:
select_pre_fs = hyperparams[
domain_list.index(self._generate_dist_name("feature_scaling_candidate"))
]
selected = Preprocessors(category="feature_scaling").get_type(select_pre_fs)
model_list.append(selected.fqdn())
add_stage_hp(selected)
if len(self.feature_selection) > 0:
select_pre_fs = hyperparams[
domain_list.index(
self._generate_dist_name("feature_selection_candidate")
)
]
selected = Preprocessors(category="dimensionality_reduction").get_type(
select_pre_fs
)
model_list.append(selected.fqdn())
add_stage_hp(selected)
# Add data cleanup
cleaner = Preprocessors(category="dimensionality_reduction").get_type(
"data_cleanup"
)
model_list.append(cleaner.fqdn())
# Add predictor
model_list.append(self.classifier.fqdn())
add_stage_hp(self.classifier)
log.info(f"[get_pipeline]: {model_list} -> {args}")
return model_list, args
def get_pipeline(
self, search_domains: List[params.Params], hyperparams: List
) -> PipelineMeta:
model_list, args = self.get_pipeline_template(search_domains, hyperparams)
return self.get_pipeline_from_template(model_list, args)
def get_pipeline_from_template(self, model_list: List, args: Dict) -> PipelineMeta:
return Pipeline(model_list)(args)
def get_pipeline_from_named_args(self, **kwargs: Any) -> PipelineMeta:
model_list = list()
pipeline_args: dict = {}
def add_stage_hp(plugin: Type[Plugin]) -> None:
if plugin.name() not in pipeline_args:
pipeline_args[plugin.name()] = {}
for param in plugin.hyperparameter_space_fqdn(**predefined_args):
if param.name not in kwargs:
continue
param_val = kwargs[param.name]
param_val = type(param.bounds[0])(param_val)
pipeline_args[plugin.name()][param.name.split(".")[-1]] = param_val
imputation_key = self._generate_dist_name("imputation_candidate")
if imputation_key in kwargs:
idx = kwargs[imputation_key]
selected = Imputers().get_type(idx)
model_list.append(selected.fqdn())
add_stage_hp(selected)
elif len(self.imputers) > 0:
model_list.append(self.imputers[0].fqdn())
add_stage_hp(self.imputers[0])
pre_key = self._generate_dist_name("feature_selection_candidate")
if pre_key in kwargs:
idx = kwargs[pre_key]
selected = Preprocessors(category="dimensionality_reduction").get_type(idx)
model_list.append(selected.fqdn())
add_stage_hp(selected)
pre_key = self._generate_dist_name("feature_scaling_candidate")
if pre_key in kwargs:
idx = kwargs[pre_key]
selected = Preprocessors(category="feature_scaling").get_type(idx)
model_list.append(selected.fqdn())
add_stage_hp(selected)
# Add data cleanup
cleaner = Preprocessors(category="dimensionality_reduction").get_type(
"data_cleanup"
)
model_list.append(cleaner.fqdn())
# Add predictor
model_list.append(self.classifier.fqdn())
add_stage_hp(self.classifier)
return Pipeline(model_list)(pipeline_args)