-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
classification.py
401 lines (350 loc) · 14.4 KB
/
classification.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
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
from typing import Optional, Union
import copy
from itertools import product
import numpy as np
from ConfigSpace.configuration_space import Configuration, ConfigurationSpace
from ConfigSpace.forbidden import ForbiddenAndConjunction, ForbiddenEqualsClause
from sklearn.base import ClassifierMixin
from autosklearn.askl_typing import FEAT_TYPE_TYPE
from autosklearn.pipeline.base import BasePipeline
from autosklearn.pipeline.components.classification import ClassifierChoice
from autosklearn.pipeline.components.data_preprocessing import DataPreprocessorChoice
from autosklearn.pipeline.components.data_preprocessing.balancing.balancing import (
Balancing,
)
from autosklearn.pipeline.components.feature_preprocessing import (
FeaturePreprocessorChoice,
)
from autosklearn.pipeline.constants import SPARSE
class SimpleClassificationPipeline(BasePipeline, ClassifierMixin):
"""This class implements the classification task.
It implements a pipeline, which includes one preprocessing step and one
classification algorithm. It can render a search space including all known
classification and preprocessing algorithms.
Contrary to the sklearn API it is not possible to enumerate the
possible parameters in the __init__ function because we only know the
available classifiers at runtime. For this reason the user must
specifiy the parameters by passing an instance of
ConfigSpace.configuration_space.Configuration.
Parameters
----------
config : ConfigSpace.configuration_space.Configuration
The configuration to evaluate.
random_state : Optional[int | RandomState]
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance
used by `np.random`.
Attributes
----------
_estimator : The underlying scikit-learn classification model. This
variable is assigned after a call to the
:meth:`autosklearn.pipeline.classification.SimpleClassificationPipeline
.fit` method.
_preprocessor : The underlying scikit-learn preprocessing algorithm. This
variable is only assigned if a preprocessor is specified and
after a call to the
:meth:`autosklearn.pipeline.classification.SimpleClassificationPipeline
.fit` method.
See also
--------
References
----------
Examples
--------
"""
def __init__(
self,
config: Optional[Configuration] = None,
feat_type: Optional[FEAT_TYPE_TYPE] = None,
steps=None,
dataset_properties=None,
include=None,
exclude=None,
random_state: Optional[Union[int, np.random.RandomState]] = None,
init_params=None,
):
self._output_dtype = np.int32
if dataset_properties is None:
dataset_properties = dict()
if "target_type" not in dataset_properties:
dataset_properties["target_type"] = "classification"
super().__init__(
feat_type=feat_type,
config=config,
steps=steps,
dataset_properties=dataset_properties,
include=include,
exclude=exclude,
random_state=random_state,
init_params=init_params,
)
def fit_transformer(self, X, y, fit_params=None):
if fit_params is None:
fit_params = {}
if self.config["balancing:strategy"] == "weighting":
balancing = Balancing(strategy="weighting")
_init_params, _fit_params = balancing.get_weights(
y,
self.config["classifier:__choice__"],
self.config["feature_preprocessor:__choice__"],
{},
{},
)
_init_params.update(self.init_params)
self.set_hyperparameters(
feat_type=self.feat_type,
configuration=self.config,
init_params=_init_params,
)
if _fit_params is not None:
fit_params.update(_fit_params)
X, fit_params = super().fit_transformer(X, y, fit_params=fit_params)
return X, fit_params
def predict_proba(self, X, batch_size=None):
"""predict_proba.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
batch_size: int or None, defaults to None
batch_size controls whether the pipeline will be
called on small chunks of the data. Useful when calling the
predict method on the whole array X results in a MemoryError.
Returns
-------
array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
"""
if batch_size is None:
return super().predict_proba(X)
else:
if not isinstance(batch_size, int):
raise ValueError(
"Argument 'batch_size' must be of type int, "
"but is '%s'" % type(batch_size)
)
if batch_size <= 0:
raise ValueError(
"Argument 'batch_size' must be positive, " "but is %d" % batch_size
)
else:
# Probe for the target array dimensions
target = self.predict_proba(X[0:2].copy())
y = np.zeros((X.shape[0], target.shape[1]), dtype=np.float32)
for k in range(max(1, int(np.ceil(float(X.shape[0]) / batch_size)))):
batch_from = k * batch_size
batch_to = min([(k + 1) * batch_size, X.shape[0]])
pred_prob = self.predict_proba(
X[batch_from:batch_to], batch_size=None
)
y[batch_from:batch_to] = pred_prob.astype(np.float32)
return y
def _get_hyperparameter_search_space(
self,
feat_type: Optional[FEAT_TYPE_TYPE] = None,
include=None,
exclude=None,
dataset_properties=None,
):
"""Create the hyperparameter configuration space.
Parameters
----------
feat_type : dict, maps columns to there datatypes
include : dict (optional, default=None)
Returns
-------
cs : ConfigSpace.configuration_space.Configuration
The configuration space describing the SimpleRegressionClassifier.
"""
cs = ConfigurationSpace()
if dataset_properties is None or not isinstance(dataset_properties, dict):
dataset_properties = dict()
if "target_type" not in dataset_properties:
dataset_properties["target_type"] = "classification"
if dataset_properties["target_type"] != "classification":
dataset_properties["target_type"] = "classification"
if "sparse" not in dataset_properties:
# This dataset is probably dense
dataset_properties["sparse"] = False
cs = self._get_base_search_space(
cs=cs,
feat_type=feat_type,
dataset_properties=dataset_properties,
exclude=exclude,
include=include,
pipeline=self.steps,
)
classifiers = cs.get_hyperparameter("classifier:__choice__").choices
preprocessors = cs.get_hyperparameter("feature_preprocessor:__choice__").choices
available_classifiers = self._final_estimator.get_available_components(
dataset_properties
)
possible_default_classifier = copy.copy(list(available_classifiers.keys()))
default = cs.get_hyperparameter("classifier:__choice__").default_value
del possible_default_classifier[possible_default_classifier.index(default)]
# A classifier which can handle sparse data after the densifier is
# forbidden for memory issues
for key in classifiers:
if SPARSE in available_classifiers[key].get_properties()["input"]:
if "densifier" in preprocessors:
while True:
try:
forb_cls = ForbiddenEqualsClause(
cs.get_hyperparameter("classifier:__choice__"), key
)
forb_fpp = ForbiddenEqualsClause(
cs.get_hyperparameter(
"feature_preprocessor:__choice__"
),
"densifier",
)
cs.add_forbidden_clause(
ForbiddenAndConjunction(forb_cls, forb_fpp)
)
# Success
break
except ValueError:
# Change the default and try again
try:
default = possible_default_classifier.pop()
except IndexError:
raise ValueError(
"Cannot find a legal default configuration."
)
cs.get_hyperparameter(
"classifier:__choice__"
).default_value = default
# which would take too long
# Combinations of non-linear models with feature learning:
classifiers_ = [
"adaboost",
"decision_tree",
"extra_trees",
"gradient_boosting",
"k_nearest_neighbors",
"libsvm_svc",
"mlp",
"random_forest",
"gaussian_nb",
]
feature_learning = [
"kernel_pca",
"kitchen_sinks",
"nystroem_sampler",
]
for c, f in product(classifiers_, feature_learning):
if c not in classifiers:
continue
if f not in preprocessors:
continue
while True:
try:
cs.add_forbidden_clause(
ForbiddenAndConjunction(
ForbiddenEqualsClause(
cs.get_hyperparameter("classifier:__choice__"), c
),
ForbiddenEqualsClause(
cs.get_hyperparameter(
"feature_preprocessor:__choice__"
),
f,
),
)
)
break
except KeyError:
break
except ValueError:
# Change the default and try again
try:
default = possible_default_classifier.pop()
except IndexError:
raise ValueError("Cannot find a legal default configuration.")
cs.get_hyperparameter(
"classifier:__choice__"
).default_value = default
# Won't work
# Multinomial NB etc don't use with features learning, pca etc
classifiers_ = ["multinomial_nb"]
preproc_with_negative_X = [
"kitchen_sinks",
"pca",
"truncatedSVD",
"fast_ica",
"kernel_pca",
"nystroem_sampler",
]
for c, f in product(classifiers_, preproc_with_negative_X):
if c not in classifiers:
continue
if f not in preprocessors:
continue
while True:
try:
cs.add_forbidden_clause(
ForbiddenAndConjunction(
ForbiddenEqualsClause(
cs.get_hyperparameter(
"feature_preprocessor:__choice__"
),
f,
),
ForbiddenEqualsClause(
cs.get_hyperparameter("classifier:__choice__"), c
),
)
)
break
except KeyError:
break
except ValueError:
# Change the default and try again
try:
default = possible_default_classifier.pop()
except IndexError:
raise ValueError("Cannot find a legal default configuration.")
cs.get_hyperparameter(
"classifier:__choice__"
).default_value = default
self.configuration_space = cs
self.dataset_properties = dataset_properties
return cs
def _get_pipeline_steps(
self, dataset_properties, feat_type: Optional[FEAT_TYPE_TYPE] = None
):
steps = []
default_dataset_properties = {"target_type": "classification"}
if dataset_properties is not None and isinstance(dataset_properties, dict):
default_dataset_properties.update(dataset_properties)
steps.extend(
[
[
"data_preprocessor",
DataPreprocessorChoice(
feat_type=feat_type,
dataset_properties=default_dataset_properties,
random_state=self.random_state,
),
],
["balancing", Balancing(random_state=self.random_state)],
[
"feature_preprocessor",
FeaturePreprocessorChoice(
feat_type=feat_type,
dataset_properties=default_dataset_properties,
random_state=self.random_state,
),
],
[
"classifier",
ClassifierChoice(
feat_type=feat_type,
dataset_properties=default_dataset_properties,
random_state=self.random_state,
),
],
]
)
return steps
def _get_estimator_hyperparameter_name(self):
return "classifier"