-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathbase.py
More file actions
453 lines (348 loc) · 14 KB
/
Copy pathbase.py
File metadata and controls
453 lines (348 loc) · 14 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from __future__ import annotations
import importlib
import inspect
import pkgutil
import sys
from collections import OrderedDict
from sklearn.base import BaseEstimator, TransformerMixin
from autosklearn.pipeline.constants import SPARSE
_addons: dict[str, ThirdPartyComponents] = {}
def find_components(package, directory, base_class):
components = OrderedDict()
for module_loader, module_name, ispkg in pkgutil.iter_modules([directory]):
full_module_name = "%s.%s" % (package, module_name)
if full_module_name not in sys.modules and not ispkg:
module = importlib.import_module(full_module_name)
for member_name, obj in inspect.getmembers(module):
if (
inspect.isclass(obj)
and issubclass(obj, base_class)
and obj != base_class
):
# TODO test if the obj implements the interface
# Keep in mind that this only instantiates the ensemble_wrapper,
# but not the real target classifier
classifier = obj
components[module_name] = classifier
return components
class ThirdPartyComponents(object):
def __init__(self, base_class):
self.base_class = base_class
self.components = OrderedDict()
def add_component(self, obj):
if inspect.isclass(obj) and self.base_class in obj.__bases__:
name = obj.__name__
classifier = obj
else:
raise TypeError(
"add_component works only with a subclass of %s" % str(self.base_class)
)
properties = set(classifier.get_properties())
should_be_there = {
"shortname",
"name",
"handles_regression",
"handles_classification",
"handles_multiclass",
"handles_multilabel",
"handles_multioutput",
"is_deterministic",
"input",
"output",
}
for property in properties:
if property not in should_be_there:
raise ValueError(
"Property %s must not be specified for "
"algorithm %s. Only the following properties "
"can be specified: %s" % (property, name, str(should_be_there))
)
for property in should_be_there:
if property not in properties:
raise ValueError(
"Property %s not specified for algorithm %s" % (property, name)
)
self.components[name] = classifier
class AutoSklearnComponent(BaseEstimator):
@staticmethod
def get_properties(dataset_properties=None):
"""Get the properties of the underlying algorithm.
Find more information at :ref:`get_properties`
Parameters
----------
dataset_properties : dict, optional (default=None)
Returns
-------
dict
"""
raise NotImplementedError()
@staticmethod
def get_hyperparameter_search_space(dataset_properties=None):
"""Return the configuration space of this classification algorithm.
Parameters
----------
dataset_properties : dict, optional (default=None)
Returns
-------
Configspace.configuration_space.ConfigurationSpace
The configuration space of this classification algorithm.
"""
raise NotImplementedError()
def fit(self, X, y):
"""The fit function calls the fit function of the underlying
scikit-learn model and returns `self`.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Training data
y : array-like, shape = (n_samples,) or shape = (n_sample, n_labels)
Returns
-------
self : returns an instance of self.
Targets
Notes
-----
Please see the `scikit-learn API documentation
<https://scikit-learn.org/stable/developers/develop.html#apis-of-scikit-learn-objects>`_
for further information."""
raise NotImplementedError()
def set_hyperparameters(self, configuration, init_params=None):
params = configuration.get_dictionary()
for param, value in params.items():
if not hasattr(self, param):
raise ValueError(
"Cannot set hyperparameter %s for %s because "
"the hyperparameter does not exist." % (param, str(self))
)
setattr(self, param, value)
if init_params is not None:
for param, value in init_params.items():
if not hasattr(self, param):
raise ValueError(
"Cannot set init param %s for %s because "
"the init param does not exist." % (param, str(self))
)
setattr(self, param, value)
return self
def __str__(self):
name = self.get_properties()["name"]
return "autosklearn.pipeline %s" % name
class IterativeComponent(AutoSklearnComponent):
def fit(self, X, y, sample_weight=None):
self.iterative_fit(X, y, n_iter=2, refit=True)
iteration = 2
while not self.configuration_fully_fitted():
n_iter = int(2**iteration / 2)
self.iterative_fit(X, y, n_iter=n_iter, refit=False)
iteration += 1
return self
@staticmethod
def get_max_iter():
raise NotImplementedError()
def get_current_iter(self):
raise NotImplementedError()
class IterativeComponentWithSampleWeight(AutoSklearnComponent):
def fit(self, X, y, sample_weight=None):
self.iterative_fit(X, y, n_iter=2, refit=True, sample_weight=sample_weight)
iteration = 2
while not self.configuration_fully_fitted():
n_iter = int(2**iteration / 2)
self.iterative_fit(
X, y, n_iter=n_iter, refit=False, sample_weight=sample_weight
)
iteration += 1
return self
@staticmethod
def get_max_iter():
raise NotImplementedError()
def get_current_iter(self):
raise NotImplementedError()
class AutoSklearnClassificationAlgorithm(AutoSklearnComponent):
"""Provide an abstract interface for classification algorithms in
auto-sklearn.
See :ref:`extending` for more information."""
def __init__(self):
self.estimator = None
self.properties = None
def predict(self, X):
"""The predict function calls the predict function of the
underlying scikit-learn model and returns an array with the predictions.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Returns
-------
array, shape = (n_samples,) or shape = (n_samples, n_labels)
Returns the predicted values
Notes
-----
Please see the `scikit-learn API documentation
<https://scikit-learn.org/stable/developers/develop.html#apis-of-scikit-learn-objects>`_
for further information."""
raise NotImplementedError()
def predict_proba(self, X):
"""Predict probabilities.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Returns
-------
array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
"""
raise NotImplementedError()
def get_estimator(self):
"""Return the underlying estimator object.
Returns
-------
estimator : the underlying estimator object
"""
return self.estimator
class AutoSklearnPreprocessingAlgorithm(TransformerMixin, AutoSklearnComponent):
"""Provide an abstract interface for preprocessing algorithms in
auto-sklearn.
See :ref:`extending` for more information."""
def __init__(self):
self.preprocessor = None
def transform(self, X):
"""The transform function calls the transform function of the
underlying scikit-learn model and returns the transformed array.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Returns
-------
X : array
Return the transformed training data
Notes
-----
Please see the `scikit-learn API documentation
<https://scikit-learn.org/stable/developers/develop.html#apis-of-scikit-learn-objects>`_
for further information."""
raise NotImplementedError()
def get_preprocessor(self):
"""Return the underlying preprocessor object.
Returns
-------
preprocessor : the underlying preprocessor object
"""
return self.preprocessor
class AutoSklearnRegressionAlgorithm(AutoSklearnComponent):
"""Provide an abstract interface for regression algorithms in
auto-sklearn.
Make a subclass of this and put it into the directory
`autosklearn/pipeline/components/regression` to make it available."""
def __init__(self):
self.estimator = None
self.properties = None
def predict(self, X):
"""The predict function calls the predict function of the
underlying scikit-learn model and returns an array with the predictions.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Returns
-------
array, shape = (n_samples,) or shape = (n_samples, n_targets)
Returns the predicted values
Notes
-----
Please see the `scikit-learn API documentation
<https://scikit-learn.org/stable/developers/develop.html#apis-of-scikit-learn-objects>`_
for further information."""
raise NotImplementedError()
def get_estimator(self):
"""Return the underlying estimator object.
Returns
-------
estimator : the underlying estimator object
"""
return self.estimator
class AutoSklearnChoice(object):
def __init__(self, dataset_properties, random_state=None):
"""
Parameters
----------
dataset_properties : dict
Describes the dataset to work on, this can change the
configuration space constructed by auto-sklearn. Mandatory
properties are:
* target_type: classification or regression
Optional properties are:
* multiclass: whether the dataset is a multiclass classification
dataset.
* multilabel: whether the dataset is a multilabel classification
dataset
"""
# Since all calls to get_hyperparameter_search_space will be done by the
# pipeline on construction, it is not necessary to construct a
# configuration space at this location!
# self.configuration = self.get_hyperparameter_search_space(
# dataset_properties).get_default_configuration()
self.random_state = random_state
# Since the pipeline will initialize the hyperparameters, it is not
# necessary to do this upon the construction of this object
# self.set_hyperparameters(self.configuration)
self.choice = None
def get_components(cls):
raise NotImplementedError()
def get_available_components(
self, dataset_properties=None, include=None, exclude=None
):
if dataset_properties is None:
dataset_properties = {}
if include is not None and exclude is not None:
raise ValueError(
"The argument include and exclude cannot be used together."
)
available_comp = self.get_components()
if include is not None:
for incl in include:
if incl not in available_comp:
raise ValueError(
"Trying to include unknown component: " "%s" % incl
)
components_dict = OrderedDict()
for name in available_comp:
if include is not None and name not in include:
continue
elif exclude is not None and name in exclude:
continue
if "sparse" in dataset_properties and dataset_properties["sparse"]:
# In case the dataset is sparse, ignore
# components that do not handle sparse data
# Auto-sklearn uses SPARSE constant as a mechanism
# to indicate whether a component can handle sparse data.
# If SPARSE is not in the input properties of the component, it
# means SPARSE is not a valid input to this component, so filter it out
if SPARSE not in available_comp[name].get_properties()["input"]:
continue
components_dict[name] = available_comp[name]
return components_dict
def set_hyperparameters(self, configuration, init_params=None):
new_params = {}
params = configuration.get_dictionary()
choice = params["__choice__"]
del params["__choice__"]
for param, value in params.items():
param = param.replace(choice, "").replace(":", "")
new_params[param] = value
if init_params is not None:
for param, value in init_params.items():
param = param.replace(choice, "").replace(":", "")
new_params[param] = value
new_params["random_state"] = self.random_state
self.new_params = new_params
self.choice = self.get_components()[choice](**new_params)
return self
def get_hyperparameter_search_space(
self, dataset_properties=None, default=None, include=None, exclude=None
):
raise NotImplementedError()
def fit(self, X, y, **kwargs):
# Allows to use check_is_fitted on the choice object
self.fitted_ = True
if kwargs is None:
kwargs = {}
return self.choice.fit(X, y, **kwargs)
def predict(self, X):
return self.choice.predict(X)