-
Notifications
You must be signed in to change notification settings - Fork 89
/
_rotation_forest_classifier.py
528 lines (441 loc) · 18.8 KB
/
_rotation_forest_classifier.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
# -*- coding: utf-8 -*-
"""A Rotation Forest (RotF) vector classifier.
A Rotation Forest aeon implementation for continuous values only. Fits sklearn
conventions.
"""
__author__ = ["MatthewMiddlehurst"]
__all__ = ["RotationForestClassifier"]
import time
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from sklearn.base import BaseEstimator
from sklearn.decomposition import PCA
from sklearn.tree import DecisionTreeClassifier
from sklearn.utils import check_random_state
from aeon.base._base import _clone_estimator
from aeon.exceptions import NotFittedError
from aeon.utils.validation import check_n_jobs
class RotationForestClassifier(BaseEstimator):
"""
A rotation forest (RotF) vector classifier.
Implementation of the Rotation Forest classifier described [1]_. Builds a forest
of trees build on random portions of the data transformed using PCA.
Intended as a benchmark for time series data and a base classifier for
transformation based appraoches such as ShapeletTransformClassifier, this aeon
implementation only works with continuous attributes.
Parameters
----------
n_estimators : int, default=200
Number of estimators to build for the ensemble.
min_group : int, default=3
The minimum size of an attribute subsample group.
max_group : int, default=3
The maximum size of an attribute subsample group.
remove_proportion : float, default=0.5
The proportion of cases to be removed per group.
base_estimator : BaseEstimator or None, default="None"
Base estimator for the ensemble. By default, uses the sklearn
`DecisionTreeClassifier` using entropy as a splitting measure.
time_limit_in_minutes : int, default=0
Time contract to limit build time in minutes, overriding ``n_estimators``.
Default of `0` means ``n_estimators`` is used.
contract_max_n_estimators : int, default=500
Max number of estimators to build when ``time_limit_in_minutes`` is set.
save_transformed_data : bool, default=False
Save the data transformed in fit in ``transformed_data_`` for use in
``_get_train_probs``.
n_jobs : int, default=1
The number of jobs to run in parallel for both ``fit`` and ``predict``.
`-1` means using all processors.
random_state : int, RandomState instance or None, default=None
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
----------
classes_ : list
The unique class labels in the training set.
n_classes_ : int
The number of unique classes in the training set.
n_instances_ : int
The number of train cases in the training set.
n_atts_ : int
The number of attributes in the training set.
transformed_data_ : list of shape (n_estimators) of ndarray
The transformed training dataset for all classifiers. Only saved when
``save_transformed_data`` is `True`.
estimators_ : list of shape (n_estimators) of BaseEstimator
The collections of estimators trained in fit.
See Also
--------
ShapeletTransformClassifier: A shapelet-based classifier using Rotation Forest.
Notes
-----
For the Java version, see
`tsml <https://github.com/uea-machine-learning/tsml/blob/master/src/main/java
/weka/classifiers/meta/RotationForest.java>`_.
References
----------
.. [1] Rodriguez, Juan José, Ludmila I. Kuncheva, and Carlos J. Alonso. "Rotation
forest: A new classifier ensemble method." IEEE transactions on pattern analysis
and machine intelligence 28.10 (2006).
.. [2] Bagnall, A., et al. "Is rotation forest the best classifier for problems
with continuous features?." arXiv preprint arXiv:1809.06705 (2018).
Examples
--------
>>> from aeon.classification.sklearn import RotationForestClassifier
>>> from aeon.datasets import make_example_2d_numpy
>>> X, y = make_example_2d_numpy(n_cases=10, n_timepoints=12,
... return_y=True, random_state=0)
>>> clf = RotationForestClassifier(n_estimators=10)
>>> clf.fit(X, y)
RotationForestClassifier(n_estimators=10)
>>> clf.predict(X)
array([0, 1, 0, 1, 0, 0, 1, 1, 1, 0])
"""
def __init__(
self,
n_estimators=200,
min_group=3,
max_group=3,
remove_proportion=0.5,
base_estimator=None,
time_limit_in_minutes=0.0,
contract_max_n_estimators=500,
save_transformed_data=False,
n_jobs=1,
random_state=None,
):
self.n_estimators = n_estimators
self.min_group = min_group
self.max_group = max_group
self.remove_proportion = remove_proportion
self.base_estimator = base_estimator
self.time_limit_in_minutes = time_limit_in_minutes
self.contract_max_n_estimators = contract_max_n_estimators
self.save_transformed_data = save_transformed_data
self.n_jobs = n_jobs
self.random_state = random_state
super(RotationForestClassifier, self).__init__()
def fit(self, X, y):
"""Fit a forest of trees on cases (X,y), where y is the target variable.
Parameters
----------
X : 2d ndarray or DataFrame of shape = [n_instances, n_attributes]
The training data.
y : array-like, shape = [n_instances]
The class labels.
Returns
-------
self :
Reference to self.
Notes
-----
Changes state by creating a fitted model that updates attributes
ending in "_".
"""
if isinstance(X, np.ndarray) and len(X.shape) == 3 and X.shape[1] == 1:
X = np.reshape(X, (X.shape[0], -1))
elif isinstance(X, pd.DataFrame) and len(X.shape) == 2:
X = X.to_numpy()
elif not isinstance(X, np.ndarray) or len(X.shape) > 2:
raise ValueError(
"RotationForestClassifier is not a time series classifier. "
"A valid sklearn input such as a 2d numpy array is required."
"Sparse input formats are currently not supported."
)
X, y = self._validate_data(X=X, y=y, ensure_min_samples=2)
self._n_jobs = check_n_jobs(self.n_jobs)
self.n_instances_, self.n_atts_ = X.shape
self.classes_ = np.unique(y)
self.n_classes_ = self.classes_.shape[0]
self._class_dictionary = {}
for index, classVal in enumerate(self.classes_):
self._class_dictionary[classVal] = index
# escape if only one class seen
if self.n_classes_ == 1:
self._is_fitted = True
return self
time_limit = self.time_limit_in_minutes * 60
start_time = time.time()
train_time = 0
if self.base_estimator is None:
self._base_estimator = DecisionTreeClassifier(criterion="entropy")
# remove useless attributes
self._useful_atts = ~np.all(X[1:] == X[:-1], axis=0)
X = X[:, self._useful_atts]
self._n_atts = X.shape[1]
# normalise attributes
self._min = X.min(axis=0)
self._ptp = X.max(axis=0) - self._min
X = (X - self._min) / self._ptp
X_cls_split = [X[np.where(y == i)] for i in self.classes_]
rng = check_random_state(self.random_state)
if time_limit > 0:
self._n_estimators = 0
self.estimators_ = []
self._pcas = []
self._groups = []
self.transformed_data_ = []
while (
train_time < time_limit
and self._n_estimators < self.contract_max_n_estimators
):
fit = Parallel(n_jobs=self._n_jobs, prefer="threads")(
delayed(self._fit_estimator)(
X,
X_cls_split,
y,
check_random_state(rng.randint(np.iinfo(np.int32).max)),
)
for _ in range(self._n_jobs)
)
estimators, pcas, groups, transformed_data = zip(*fit)
self.estimators_ += estimators
self._pcas += pcas
self._groups += groups
self.transformed_data_ += transformed_data
self._n_estimators += self._n_jobs
train_time = time.time() - start_time
else:
self._n_estimators = self.n_estimators
fit = Parallel(n_jobs=self._n_jobs, prefer="threads")(
delayed(self._fit_estimator)(
X,
X_cls_split,
y,
check_random_state(rng.randint(np.iinfo(np.int32).max)),
)
for _ in range(self._n_estimators)
)
self.estimators_, self._pcas, self._groups, self.transformed_data_ = zip(
*fit
)
self._is_fitted = True
return self
def predict(self, X):
"""Predict for all cases in X. Built on top of predict_proba.
Parameters
----------
X : 2d ndarray or DataFrame of shape = [n_instances, n_attributes]
The data to make predictions for.
Returns
-------
y : array-like, shape = [n_instances]
Predicted class labels.
"""
rng = check_random_state(self.random_state)
return np.array(
[
self.classes_[int(rng.choice(np.flatnonzero(prob == prob.max())))]
for prob in self.predict_proba(X)
]
)
def predict_proba(self, X):
"""Probability estimates for each class for all cases in X.
Parameters
----------
X : 2d ndarray or DataFrame of shape = [n_instances, n_attributes]
The data to make predictions for.
Returns
-------
y : array-like, shape = [n_instances, n_classes_]
Predicted probabilities using the ordering in classes_.
"""
if not self._is_fitted:
raise NotFittedError(
f"This instance of {self.__class__.__name__} has not "
f"been fitted yet; please call `fit` first."
)
# treat case of single class seen in fit
if self.n_classes_ == 1:
return np.repeat([[1]], X.shape[0], axis=0)
if isinstance(X, np.ndarray) and len(X.shape) == 3 and X.shape[1] == 1:
X = np.reshape(X, (X.shape[0], -1))
elif isinstance(X, pd.DataFrame) and len(X.shape) == 2:
X = X.to_numpy()
elif not isinstance(X, np.ndarray) or len(X.shape) > 2:
raise ValueError(
"RotationForestClassifier is not a time series classifier. "
"A valid sklearn input such as a 2d numpy array is required."
"Sparse input formats are currently not supported."
)
X = self._validate_data(X=X, reset=False)
# replace missing values with 0 and remove useless attributes
X = X[:, self._useful_atts]
# normalise the data.
X = (X - self._min) / self._ptp
y_probas = Parallel(n_jobs=self._n_jobs, prefer="threads")(
delayed(self._predict_proba_for_estimator)(
X,
self.estimators_[i],
self._pcas[i],
self._groups[i],
)
for i in range(self._n_estimators)
)
output = np.sum(y_probas, axis=0) / (
np.ones(self.n_classes_) * self._n_estimators
)
return output
def _get_train_probs(self, X, y):
if not self._is_fitted:
raise NotFittedError(
f"This instance of {self.__class__.__name__} has not "
f"been fitted yet; please call `fit` first."
)
if isinstance(X, np.ndarray) and len(X.shape) == 3 and X.shape[1] == 1:
X = np.reshape(X, (X.shape[0], -1))
elif isinstance(X, pd.DataFrame) and len(X.shape) == 2:
X = X.to_numpy()
elif not isinstance(X, np.ndarray) or len(X.shape) > 2:
raise ValueError(
"RotationForestClassifier is not a time series classifier. "
"A valid sklearn input such as a 2d numpy array is required."
"Sparse input formats are currently not supported."
)
X = self._validate_data(X=X, reset=False)
# handle the single-class-label case
if len(self._class_dictionary) == 1:
return np.repeat([[1]], len(X), axis=0)
n_instances, n_atts = X.shape
if n_instances != self.n_instances_ or n_atts != self.n_atts_:
raise ValueError(
"n_instances, n_atts mismatch. X should be the same as the training "
"data used in fit for generating train probabilities."
)
if not self.save_transformed_data:
raise ValueError("Currently only works with saved transform data from fit.")
rng = check_random_state(self.random_state)
p = Parallel(n_jobs=self._n_jobs, prefer="threads")(
delayed(self._train_probas_for_estimator)(
y,
i,
check_random_state(rng.randint(np.iinfo(np.int32).max)),
)
for i in range(self._n_estimators)
)
y_probas, oobs = zip(*p)
results = np.sum(y_probas, axis=0)
divisors = np.zeros(n_instances)
for oob in oobs:
for inst in oob:
divisors[inst] += 1
for i in range(n_instances):
results[i] = (
np.ones(self.n_classes_) * (1 / self.n_classes_)
if divisors[i] == 0
else results[i] / (np.ones(self.n_classes_) * divisors[i])
)
return results
def _fit_estimator(self, X, X_cls_split, y, rng):
groups = self._generate_groups(rng)
pcas = []
# construct the slices to fit the PCAs too.
for group in groups:
classes = rng.choice(
range(self.n_classes_),
size=rng.randint(1, self.n_classes_ + 1),
replace=False,
)
# randomly add the classes with the randomly selected attributes.
X_t = np.zeros((0, len(group)))
for cls_idx in classes:
c = X_cls_split[cls_idx]
X_t = np.concatenate((X_t, c[:, group]), axis=0)
sample_ind = rng.choice(
X_t.shape[0],
max(1, int(X_t.shape[0] * self.remove_proportion)),
replace=False,
)
X_t = X_t[sample_ind]
# try to fit the PCA if it fails, remake it, and add 10 random data
# instances.
while True:
# ignore err state on PCA because we account if it fails.
with np.errstate(divide="ignore", invalid="ignore"):
# differences between os occasionally. seems to happen when there
# are low amounts of cases in the fit
pca = PCA(random_state=rng).fit(X_t)
if not np.isnan(pca.explained_variance_ratio_).all():
break
X_t = np.concatenate(
(X_t, rng.random_sample((10, X_t.shape[1]))), axis=0
)
pcas.append(pca)
# merge all the pca_transformed data into one instance and build a classifier
# on it.
X_t = np.concatenate(
[pcas[i].transform(X[:, group]) for i, group in enumerate(groups)], axis=1
)
X_t = X_t.astype(np.float32)
X_t = np.nan_to_num(
X_t, False, 0, np.finfo(np.float32).max, np.finfo(np.float32).min
)
tree = _clone_estimator(self._base_estimator, random_state=rng)
tree.fit(X_t, y)
return tree, pcas, groups, X_t if self.save_transformed_data else None
def _predict_proba_for_estimator(self, X, clf, pcas, groups):
X_t = np.concatenate(
[pcas[i].transform(X[:, group]) for i, group in enumerate(groups)], axis=1
)
X_t = X_t.astype(np.float32)
X_t = np.nan_to_num(
X_t, False, 0, np.finfo(np.float32).max, np.finfo(np.float32).min
)
probas = clf.predict_proba(X_t)
if probas.shape[1] != self.n_classes_:
new_probas = np.zeros((probas.shape[0], self.n_classes_))
for i, cls in enumerate(clf.classes_):
cls_idx = self._class_dictionary[cls]
new_probas[:, cls_idx] = probas[:, i]
probas = new_probas
return probas
def _train_probas_for_estimator(self, y, idx, rng):
indices = range(self.n_instances_)
subsample = rng.choice(self.n_instances_, size=self.n_instances_)
oob = [n for n in indices if n not in subsample]
results = np.zeros((self.n_instances_, self.n_classes_))
if len(oob) == 0:
return [results, oob]
clf = _clone_estimator(self._base_estimator, rng)
clf.fit(self.transformed_data_[idx][subsample], y[subsample])
probas = clf.predict_proba(self.transformed_data_[idx][oob])
if probas.shape[1] != self.n_classes_:
new_probas = np.zeros((probas.shape[0], self.n_classes_))
for i, cls in enumerate(clf.classes_):
cls_idx = self._class_dictionary[cls]
new_probas[:, cls_idx] = probas[:, i]
probas = new_probas
for n, proba in enumerate(probas):
results[oob[n]] += proba
return [results, oob]
def _generate_groups(self, rng):
permutation = rng.permutation((np.arange(0, self._n_atts)))
# select the size of each group.
group_size_count = np.zeros(self.max_group - self.min_group + 1)
n_attributes = 0
n_groups = 0
while n_attributes < self._n_atts:
n = rng.randint(group_size_count.shape[0])
group_size_count[n] += 1
n_attributes += self.min_group + n
n_groups += 1
groups = []
current_attribute = 0
current_size = 0
for i in range(0, n_groups):
while group_size_count[current_size] == 0:
current_size += 1
group_size_count[current_size] -= 1
n = self.min_group + current_size
groups.append(np.zeros(n, dtype=int))
for k in range(0, n):
if current_attribute < permutation.shape[0]:
groups[i][k] = permutation[current_attribute]
else:
groups[i][k] = permutation[rng.randint(permutation.shape[0])]
current_attribute += 1
return groups