/
bagging_compatible.py
532 lines (428 loc) · 17 KB
/
bagging_compatible.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""CompatibleBaggingClassifier: Re-implements Bagging in imbens style.
"""
# Authors: Zhining Liu <zhining.liu@outlook.com>
# License: MIT
# %%
LOCAL_DEBUG = False
if not LOCAL_DEBUG:
from ...utils._docstring import (
FuncGlossarySubstitution,
FuncSubstitution,
Substitution,
_get_example_docstring,
_get_parameter_docstring,
)
from ...utils._validation import _deprecate_positional_args, check_target_type
from ...utils._validation_data import check_eval_datasets
from ...utils._validation_param import check_eval_metrics, check_train_verbose
from .._bagging import _parallel_build_estimators
from ..base import MAX_INT, ImbalancedEnsembleClassifierMixin
else: # pragma: no cover
import sys # For local test
sys.path.append("../..")
from ensemble.base import ImbalancedEnsembleClassifierMixin, MAX_INT
from ensemble._bagging import _parallel_build_estimators
from utils._validation_data import check_eval_datasets
from utils._validation_param import check_train_verbose, check_eval_metrics
from utils._validation import _deprecate_positional_args, check_target_type
from utils._docstring import (
Substitution,
FuncSubstitution,
FuncGlossarySubstitution,
_get_parameter_docstring,
_get_example_docstring,
)
import itertools
import numbers
from collections import Counter
from warnings import warn
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble._base import _partition_estimators
from sklearn.utils import check_random_state
from sklearn.utils.parallel import Parallel, delayed
from sklearn.utils.validation import _check_sample_weight
# Properties
_method_name = 'CompatibleBaggingClassifier'
_properties = {
'ensemble_type': 'bagging',
'training_type': 'parallel',
}
_super = BaggingClassifier
@Substitution(
random_state=_get_parameter_docstring('random_state', **_properties),
n_jobs=_get_parameter_docstring('n_jobs', **_properties),
warm_start=_get_parameter_docstring('warm_start', **_properties),
example=_get_example_docstring(_method_name),
)
class CompatibleBaggingClassifier(ImbalancedEnsembleClassifierMixin, BaggingClassifier):
"""Bagging classifier re-implemented in imbalanced-ensemble style.
A Bagging classifier is an ensemble meta-estimator that fits base
classifiers each on random subsets of the original dataset and then
aggregate their individual predictions (either by voting or by averaging)
to form a final prediction. Such a meta-estimator can typically be used as
a way to reduce the variance of a black-box estimator (e.g., a decision
tree), by introducing randomization into its construction procedure and
then making an ensemble out of it.
This algorithm encompasses several works from the literature. When random
subsets of the dataset are drawn as random subsets of the samples, then
this algorithm is known as Pasting [1]_. If samples are drawn with
replacement, then the method is known as Bagging [2]_. When random subsets
of the dataset are drawn as random subsets of the features, then the method
is known as Random Subspaces [3]_. Finally, when base estimators are built
on subsets of both samples and features, then the method is known as
Random Patches [4]_.
Parameters
----------
estimator : object, default=None
The base estimator to fit on random subsets of the dataset.
If None, then the base estimator is a
:class:`~sklearn.tree.DecisionTreeClassifier`.
n_estimators : int, default=50
The number of base estimators in the ensemble.
max_samples : int or float, default=1.0
The number of samples to draw from X to train each base estimator (with
replacement by default, see `bootstrap` for more details).
- If ``int``, then draw `max_samples` samples.
- If ``float``, then draw `max_samples * X.shape[0]` samples.
max_features : int or float, default=1.0
The number of features to draw from X to train each base estimator (
without replacement by default, see `bootstrap_features` for more
details).
- If ``int``, then draw `max_features` features.
- If ``float``, then draw `max_features * X.shape[1]` features.
bootstrap : bool, default=True
Whether samples are drawn with replacement. If False, sampling
without replacement is performed.
bootstrap_features : bool, default=False
Whether features are drawn with replacement.
oob_score : bool, default=False
Whether to use out-of-bag samples to estimate
the generalization error.
{warm_start}
{n_jobs}
{random_state}
verbose : int, default=0
Controls the verbosity when fitting and predicting.
Attributes
----------
estimator_ : estimator
The base estimator from which the ensemble is grown.
n_features_in_ : int
The number of features when :meth:`fit` is performed.
estimators_ : list of estimators
The collection of fitted base estimators.
estimators_samples_ : list of arrays
The subset of drawn samples (i.e., the in-bag samples) for each base
estimator. Each subset is defined by an array of the indices selected.
estimators_features_ : list of arrays
The subset of drawn features for each base estimator.
estimators_n_training_samples_ : list of ints
The number of training samples for each fitted
base estimators.
classes_ : ndarray of shape (n_classes,)
The classes labels.
n_classes_ : int or list
The number of classes.
oob_score_ : float
Score of the training dataset obtained using an out-of-bag estimate.
This attribute exists only when ``oob_score`` is True.
oob_decision_function_ : ndarray of shape (n_samples, n_classes)
Decision function computed with out-of-bag estimate on the training
set. If n_estimators is small it might be possible that a data point
was never left out during the bootstrap. In this case,
`oob_decision_function_` might contain NaN. This attribute exists
only when ``oob_score`` is True.
See Also
--------
CompatibleAdaBoostClassifier : AdaBoost re-implemented in imbalanced-ensemble style.
References
----------
.. [1] L. Breiman, "Pasting small votes for classification in large
databases and on-line", Machine Learning, 36(1), 85-103, 1999.
.. [2] L. Breiman, "Bagging predictors", Machine Learning, 24(2), 123-140,
1996.
.. [3] T. Ho, "The random subspace method for constructing decision
forests", Pattern Analysis and Machine Intelligence, 20(8), 832-844,
1998.
.. [4] G. Louppe and P. Geurts, "Ensembles on Random Patches", Machine
Learning and Knowledge Discovery in Databases, 346-361, 2012.
Examples
--------
{example}
"""
@_deprecate_positional_args
def __init__(
self,
estimator=None,
n_estimators: int = 50,
*,
max_samples=1.0,
max_features=1.0,
bootstrap=True,
bootstrap_features=False,
oob_score=False,
warm_start=False,
n_jobs=None,
random_state=None,
verbose=0,
):
super().__init__(
estimator=estimator,
n_estimators=n_estimators,
max_samples=max_samples,
max_features=max_features,
bootstrap=bootstrap,
bootstrap_features=bootstrap_features,
oob_score=oob_score,
warm_start=warm_start,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
)
self.__name__ = _method_name
self._properties = _properties
@_deprecate_positional_args
@FuncSubstitution(
eval_datasets=_get_parameter_docstring('eval_datasets'),
eval_metrics=_get_parameter_docstring('eval_metrics'),
train_verbose=_get_parameter_docstring('train_verbose', **_properties),
)
def fit(
self,
X,
y,
*,
sample_weight=None,
max_samples=None,
eval_datasets: dict = None,
eval_metrics: dict = None,
train_verbose: bool or int or dict = False,
):
"""Build a Bagging ensemble of estimators from the training set (X, y).
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only if
they are supported by the base estimator.
y : array-like of shape (n_samples,)
The target values (class labels).
sample_weight : array-like of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted.
Note that this is supported only if the base estimator supports
sample weighting.
max_samples : int or float, default=None
Argument to use instead of self.max_samples.
%(eval_datasets)s
%(eval_metrics)s
%(train_verbose)s
Returns
-------
self : object
"""
check_target_type(y)
random_state = check_random_state(self.random_state)
# Convert data (X is required to be 2d and indexable)
check_x_y_args = {
'accept_sparse': ['csr', 'csc'],
'dtype': None,
'force_all_finite': False,
'multi_output': True,
}
X, y = self._validate_data(X, y, **check_x_y_args)
# Check evaluation data
self.eval_datasets_ = check_eval_datasets(eval_datasets, X, y, **check_x_y_args)
# Check evaluation metrics
self.eval_metrics_ = check_eval_metrics(eval_metrics)
# Check verbose
self.train_verbose_ = check_train_verbose(
train_verbose, self.n_estimators, **self._properties
)
self._init_training_log_format()
if sample_weight is not None:
sample_weight = _check_sample_weight(sample_weight, X, dtype=None)
# Remap output
n_samples, self.n_features_in_ = X.shape
self._n_samples = n_samples
y = self._validate_y(y)
# Check parameters
self._validate_estimator()
# Validate max_samples
if max_samples is None:
max_samples = self.max_samples
if not isinstance(max_samples, numbers.Integral):
max_samples = int(max_samples * X.shape[0])
if not (0 < max_samples <= X.shape[0]):
raise ValueError("max_samples must be in (0, n_samples]")
# Store validated integer row sampling value
self._max_samples = max_samples
# Validate max_features
if isinstance(self.max_features, numbers.Integral):
max_features = self.max_features
elif isinstance(self.max_features, float):
max_features = self.max_features * self.n_features_in_
else:
raise ValueError("max_features must be int or float")
if not (0 < max_features <= self.n_features_in_):
raise ValueError("max_features must be in (0, n_features]")
max_features = max(1, int(max_features))
# Store validated integer feature sampling value
self._max_features = max_features
# Other checks
if not self.bootstrap and self.oob_score:
raise ValueError(
"Out of bag estimation only available" " if bootstrap=True"
)
if self.warm_start and self.oob_score:
raise ValueError(
"Out of bag estimate only available" " if warm_start=False"
)
if hasattr(self, "oob_score_") and self.warm_start:
del self.oob_score_
if not self.warm_start or not hasattr(self, 'estimators_'):
# Free allocated memory, if any
self.estimators_ = []
self.estimators_features_ = []
self.estimators_n_training_samples_ = []
n_more_estimators = self.n_estimators - len(self.estimators_)
if n_more_estimators < 0:
raise ValueError(
'n_estimators=%d must be larger or equal to '
'len(estimators_)=%d when warm_start==True'
% (self.n_estimators, len(self.estimators_))
)
elif n_more_estimators == 0:
warn(
"Warm-start fitting without increasing n_estimators does not "
"fit new trees."
)
return self
# Parallel loop
n_jobs, n_estimators, starts = _partition_estimators(
n_more_estimators, self.n_jobs
)
total_n_estimators = sum(n_estimators)
# Advance random state to state after training
# the first n_estimators
if self.warm_start and len(self.estimators_) > 0:
random_state.randint(MAX_INT, size=len(self.estimators_))
seeds = random_state.randint(MAX_INT, size=n_more_estimators)
self._seeds = seeds
all_results = Parallel(
n_jobs=n_jobs, verbose=self.verbose, **self._parallel_args()
)(
delayed(_parallel_build_estimators)(
n_estimators[i],
self,
X,
y,
sample_weight,
seeds[starts[i] : starts[i + 1]],
total_n_estimators,
verbose=self.verbose,
)
for i in range(n_jobs)
)
# Reduce
self.estimators_ += list(
itertools.chain.from_iterable(t[0] for t in all_results)
)
self.estimators_features_ += list(
itertools.chain.from_iterable(t[1] for t in all_results)
)
self.estimators_n_training_samples_ += list(
itertools.chain.from_iterable(t[2] for t in all_results)
)
if self.oob_score:
self._set_oob_score(X, y)
# Print training infomation to console.
self._training_log_to_console()
return self
@FuncGlossarySubstitution(_super.predict_proba, 'classes_')
def predict_proba(self, X):
return super().predict_proba(X)
@FuncGlossarySubstitution(_super.predict_log_proba, 'classes_')
def predict_log_proba(self, X):
return super().predict_log_proba(X)
def set_params(self, **params):
return super().set_params(**params)
# %%
if __name__ == "__main__": # pragma: no cover
from collections import Counter
from copy import copy
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score, balanced_accuracy_score, f1_score
from sklearn.model_selection import train_test_split
# X, y = make_classification(n_classes=2, class_sep=2, # 2-class
# weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0,
# n_features=20, n_clusters_per_class=1, n_samples=1000, random_state=10)
X, y = make_classification(
n_classes=3,
class_sep=2, # 3-class
weights=[0.1, 0.3, 0.6],
n_informative=3,
n_redundant=1,
flip_y=0,
n_features=20,
n_clusters_per_class=1,
n_samples=2000,
random_state=10,
)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.5, random_state=42
)
origin_distr = dict(Counter(y_train)) # {2: 600, 1: 300, 0: 100}
print('Original training dataset shape %s' % origin_distr)
target_distr = {2: 200, 1: 100, 0: 100}
init_kwargs_default = {
'estimator': None,
'n_estimators': 100,
'max_samples': 1.0,
'max_features': 1.0,
'bootstrap': True,
'bootstrap_features': False,
'oob_score': False,
'warm_start': False,
'n_jobs': None,
'random_state': 42,
# 'random_state': None,
'verbose': 1,
}
fit_kwargs_default = {
'X': X_train,
'y': y_train,
'eval_datasets': {'valid': (X_valid, y_valid)},
'eval_metrics': {
'acc': (accuracy_score, {}),
'balanced_acc': (balanced_accuracy_score, {}),
'weighted_f1': (f1_score, {'average': 'weighted'}),
},
'train_verbose': True,
}
ensembles = {}
init_kwargs, fit_kwargs = copy(init_kwargs_default), copy(fit_kwargs_default)
bagging_comp = CompatibleBaggingClassifier(**init_kwargs).fit(**fit_kwargs)
ensembles['bagging_comp'] = bagging_comp
# %%
from imbens.visualizer import ImbalancedEnsembleVisualizer
visualizer = ImbalancedEnsembleVisualizer(
eval_datasets=None,
eval_metrics=None,
).fit(
ensembles=ensembles,
granularity=5,
)
fig, axes = visualizer.performance_lineplot(
on_ensembles=None,
on_datasets=None,
split_by=[],
n_samples_as_x_axis=False,
sub_figsize=(4, 3.3),
sup_title=True,
alpha=0.8,
)
fig, axes = visualizer.confusion_matrix_heatmap(
on_ensembles=None,
on_datasets=None,
sub_figsize=(4, 3.3),
)
# %%