-
Notifications
You must be signed in to change notification settings - Fork 89
/
_hivecote_v2.py
460 lines (405 loc) · 16.9 KB
/
_hivecote_v2.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
"""Hierarchical Vote Collective of Transformation-based Ensembles (HIVE-COTE) V2.
Upgraded hybrid ensemble of classifiers from 4 separate time series classification
representations, using the weighted probabilistic CAWPE as an ensemble controller.
"""
__author__ = ["MatthewMiddlehurst"]
__all__ = ["HIVECOTEV2"]
from datetime import datetime
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.utils import check_random_state
from aeon.classification.base import BaseClassifier
from aeon.classification.convolution_based import Arsenal
from aeon.classification.dictionary_based import TemporalDictionaryEnsemble
from aeon.classification.interval_based._drcif import DrCIFClassifier
from aeon.classification.shapelet_based import ShapeletTransformClassifier
class HIVECOTEV2(BaseClassifier):
"""
Hierarchical Vote Collective of Transformation-based Ensembles (HIVE-COTE) V2.
An ensemble of the STC, DrCIF, Arsenal and TDE classifiers from different feature
representations using the CAWPE structure as described in [1]_.
Parameters
----------
stc_params : dict or None, default=None
Parameters for the ShapeletTransformClassifier module. If None, uses the
default parameters with a 2 hour transform contract.
drcif_params : dict or None, default=None
Parameters for the DrCIF module. If None, uses the default parameters with
n_estimators set to 500.
arsenal_params : dict or None, default=None
Parameters for the Arsenal module. If None, uses the default parameters.
tde_params : dict or None, default=None
Parameters for the TemporalDictionaryEnsemble module. If None, uses the default
parameters.
time_limit_in_minutes : int, default=0
Time contract to limit build time in minutes, overriding
n_estimators/n_parameter_samples for each component.
Default of 0 means n_estimators/n_parameter_samples for each component is used.
save_component_probas : bool, default=False
When predict/predict_proba is called, save each HIVE-COTEV2 component
probability predictions in component_probas.
verbose : int, default=0
Level of output printed to the console (for information only).
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`.
n_jobs : int, default=1
The number of jobs to run in parallel for both `fit` and `predict`.
``-1`` means using all processors.
parallel_backend : str, ParallelBackendBase instance or None, default=None
Specify the parallelisation backend implementation in joblib for Catch22,
if None a 'prefer' value of "threads" is used by default.
Valid options are "loky", "multiprocessing", "threading" or a custom backend.
See the joblib Parallel documentation for more details.
Attributes
----------
n_classes_ : int
The number of classes.
classes_ : list
The unique class labels.
stc_weight_ : float
The weight for STC probabilities.
drcif_weight_ : float
The weight for DrCIF probabilities.
arsenal_weight_ : float
The weight for Arsenal probabilities.
tde_weight_ : float
The weight for TDE probabilities.
component_probas : dict
Only used if save_component_probas is true. Saved probability predictions for
each HIVE-COTEV2 component.
See Also
--------
HIVECOTEV1, ShapeletTransformClassifier, DrCIF, Arsenal, TemporalDictionaryEnsemble
Components of HIVECOTE.
Notes
-----
For the Java version, see
`https://github.com/uea-machine-learning/tsml/blob/master/src/main/java/
tsml/classifiers/hybrids/HIVE_COTE.java`_.
References
----------
.. [1] Middlehurst, Matthew, James Large, Michael Flynn, Jason Lines, Aaron Bostrom,
and Anthony Bagnall. "HIVE-COTE 2.0: a new meta ensemble for time series
classification." Machine Learning (2021).
"""
_tags = {
"capability:multivariate": True,
"capability:contractable": True,
"capability:multithreading": True,
"algorithm_type": "hybrid",
}
def __init__(
self,
stc_params=None,
drcif_params=None,
arsenal_params=None,
tde_params=None,
time_limit_in_minutes=0,
save_component_probas=False,
verbose=0,
random_state=None,
n_jobs=1,
parallel_backend=None,
):
self.stc_params = stc_params
self.drcif_params = drcif_params
self.arsenal_params = arsenal_params
self.tde_params = tde_params
self.time_limit_in_minutes = time_limit_in_minutes
self.save_component_probas = save_component_probas
self.verbose = verbose
self.random_state = random_state
self.n_jobs = n_jobs
self.parallel_backend = parallel_backend
self.stc_weight_ = 0
self.drcif_weight_ = 0
self.arsenal_weight_ = 0
self.tde_weight_ = 0
self.component_probas = {}
self._stc_params = stc_params
self._drcif_params = drcif_params
self._arsenal_params = arsenal_params
self._tde_params = tde_params
self._stc = None
self._drcif = None
self._arsenal = None
self._tde = None
super(HIVECOTEV2, self).__init__()
_DEFAULT_N_TREES = 500
_DEFAULT_N_SHAPELETS = 10000
_DEFAULT_N_KERNELS = 2000
_DEFAULT_N_ESTIMATORS = 25
_DEFAULT_N_PARA_SAMPLES = 250
_DEFAULT_MAX_ENSEMBLE_SIZE = 50
_DEFAULT_RAND_PARAMS = 50
def _fit(self, X, y):
"""Fit HIVE-COTE 2.0 to training data.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_channels, series_length]
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 "_" and sets is_fitted flag to True.
"""
if self.stc_params is None:
self._stc_params = {"n_shapelet_samples": HIVECOTEV2._DEFAULT_N_SHAPELETS}
if self.drcif_params is None:
self._drcif_params = {"n_estimators": HIVECOTEV2._DEFAULT_N_TREES}
if self.arsenal_params is None:
self._arsenal_params = {
"num_kernels": HIVECOTEV2._DEFAULT_N_KERNELS,
"n_estimators": HIVECOTEV2._DEFAULT_N_ESTIMATORS,
}
if self.tde_params is None:
self._tde_params = {
"n_parameter_samples": HIVECOTEV2._DEFAULT_N_PARA_SAMPLES,
"max_ensemble_size": HIVECOTEV2._DEFAULT_MAX_ENSEMBLE_SIZE,
"randomly_selected_params": HIVECOTEV2._DEFAULT_RAND_PARAMS,
}
# If we are contracting split the contract time between each algorithm
if self.time_limit_in_minutes > 0:
# Leave 1/3 for train estimates
ct = self.time_limit_in_minutes / 6
self._stc_params["time_limit_in_minutes"] = ct
self._drcif_params["time_limit_in_minutes"] = ct
self._arsenal_params["time_limit_in_minutes"] = ct
self._tde_params["time_limit_in_minutes"] = ct
# Build STC
self._stc = ShapeletTransformClassifier(
**self._stc_params,
save_transformed_data=True,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._stc.fit(X, y)
if self.verbose > 0:
print("STC ", datetime.now().strftime("%H:%M:%S %d/%m/%Y")) # noqa
# Find STC weight using train set estimate
train_probs = self._stc._get_train_probs(X, y)
train_preds = self._stc.classes_[np.argmax(train_probs, axis=1)]
self.stc_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"STC train estimate ",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("STC weight = " + str(self.stc_weight_)) # noqa
# Build DrCIF
self._drcif = DrCIFClassifier(
**self._drcif_params,
save_transformed_data=True,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._drcif.fit(X, y)
if self.verbose > 0:
print("DrCIF ", datetime.now().strftime("%H:%M:%S %d/%m/%Y")) # noqa
# Find DrCIF weight using train set estimate
train_probs = self._drcif._get_train_probs(X, y)
train_preds = self._drcif.classes_[np.argmax(train_probs, axis=1)]
self.drcif_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"DrCIF train estimate ",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("DrCIF weight = " + str(self.drcif_weight_)) # noqa
# Build Arsenal
self._arsenal = Arsenal(
**self._arsenal_params,
save_transformed_data=True,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._arsenal.fit(X, y)
if self.verbose > 0:
print("Arsenal ", datetime.now().strftime("%H:%M:%S %d/%m/%Y")) # noqa
# Find Arsenal weight using train set estimate
train_probs = self._arsenal._get_train_probs(X, y)
train_preds = self._arsenal.classes_[np.argmax(train_probs, axis=1)]
self.arsenal_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"Arsenal train estimate ",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("Arsenal weight = " + str(self.arsenal_weight_)) # noqa
# Build TDE
self._tde = TemporalDictionaryEnsemble(
**self._tde_params,
save_train_predictions=True,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._tde.fit(X, y)
if self.verbose > 0:
print("TDE ", datetime.now().strftime("%H:%M:%S %d/%m/%Y")) # noqa
# Find TDE weight using train set estimate
train_probs = self._tde._get_train_probs(X, y, train_estimate_method="loocv")
train_preds = self._tde.classes_[np.argmax(train_probs, axis=1)]
self.tde_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"TDE train estimate ",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("TDE weight = " + str(self.tde_weight_)) # noqa
return self
def _predict(self, X) -> np.ndarray:
"""Predicts labels for sequences in X.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_channels, series_length]
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, return_component_probas=False) -> np.ndarray:
"""Predicts labels probabilities for sequences in X.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_channels, series_length]
The data to make predict probabilities for.
Returns
-------
y : array-like, shape = [n_instances, n_classes_]
Predicted probabilities using the ordering in classes_.
"""
dists = np.zeros((X.shape[0], self.n_classes_))
# Call predict proba on each classifier, multiply the probabilities by the
# classifiers weight then add them to the current HC2 probabilities
stc_probas = self._stc.predict_proba(X)
dists = np.add(
dists,
stc_probas * (np.ones(self.n_classes_) * self.stc_weight_),
)
drcif_probas = self._drcif.predict_proba(X)
dists = np.add(
dists,
drcif_probas * (np.ones(self.n_classes_) * self.drcif_weight_),
)
arsenal_probas = self._arsenal.predict_proba(X)
dists = np.add(
dists,
arsenal_probas * (np.ones(self.n_classes_) * self.arsenal_weight_),
)
tde_probas = self._tde.predict_proba(X)
dists = np.add(
dists,
tde_probas * (np.ones(self.n_classes_) * self.tde_weight_),
)
if self.save_component_probas:
self.component_probas = {
"STC": stc_probas,
"DrCIF": drcif_probas,
"Arsenal": arsenal_probas,
"TDE": tde_probas,
}
# Make each instances probability array sum to 1 and return
return dists / dists.sum(axis=1, keepdims=True)
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
HIVECOTEV2 provides the following special sets:
"results_comparison" - used in some classifiers to compare against
previously generated results where the default set of parameters
cannot produce suitable probability estimates
"contracting" - used in classifiers that set the
"capability:contractable" tag to True to test contacting
functionality
Returns
-------
params : dict or list of dict, default={}
Parameters to create testing instances of the class.
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`.
"""
from sklearn.ensemble import RandomForestClassifier
from aeon.classification.sklearn import RotationForestClassifier
if parameter_set == "results_comparison":
return {
"stc_params": {
"estimator": RandomForestClassifier(n_estimators=3),
"n_shapelet_samples": 50,
"max_shapelets": 5,
"batch_size": 10,
},
"drcif_params": {
"n_estimators": 3,
"n_intervals": 2,
"att_subsample_size": 2,
},
"arsenal_params": {"num_kernels": 50, "n_estimators": 3},
"tde_params": {
"n_parameter_samples": 5,
"max_ensemble_size": 3,
"randomly_selected_params": 3,
},
}
elif parameter_set == "contracting":
return {
"time_limit_in_minutes": 5,
"stc_params": {
"estimator": RotationForestClassifier(contract_max_n_estimators=1),
"contract_max_n_shapelet_samples": 5,
"max_shapelets": 5,
"batch_size": 5,
},
"drcif_params": {
"contract_max_n_estimators": 1,
"n_intervals": 2,
"att_subsample_size": 2,
},
"arsenal_params": {"num_kernels": 5, "contract_max_n_estimators": 1},
"tde_params": {
"contract_max_n_parameter_samples": 1,
"max_ensemble_size": 1,
"randomly_selected_params": 1,
},
}
else:
return {
"stc_params": {
"estimator": RandomForestClassifier(n_estimators=1),
"n_shapelet_samples": 5,
"max_shapelets": 5,
"batch_size": 5,
},
"drcif_params": {
"n_estimators": 1,
"n_intervals": 2,
"att_subsample_size": 2,
},
"arsenal_params": {"num_kernels": 5, "n_estimators": 1},
"tde_params": {
"n_parameter_samples": 1,
"max_ensemble_size": 1,
"randomly_selected_params": 1,
},
}