-
Notifications
You must be signed in to change notification settings - Fork 89
/
_hivecote_v1.py
402 lines (351 loc) · 14.4 KB
/
_hivecote_v1.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
"""Hierarchical Vote Collective of Transformation-based Ensembles (HIVE-COTE) V1.
Hybrid ensemble of classifiers from 4 separate time series classification
representations, using the weighted probabilistic CAWPE as an ensemble controller.
"""
__author__ = ["MatthewMiddlehurst"]
__all__ = ["HIVECOTEV1"]
from datetime import datetime
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_predict
from sklearn.utils import check_random_state
from aeon.classification.base import BaseClassifier
from aeon.classification.dictionary_based import ContractableBOSS
from aeon.classification.interval_based import (
RandomIntervalSpectralEnsembleClassifier,
TimeSeriesForestClassifier,
)
from aeon.classification.shapelet_based import ShapeletTransformClassifier
class HIVECOTEV1(BaseClassifier):
"""
Hierarchical Vote Collective of Transformation-based Ensembles (HIVE-COTE) V1.
An ensemble of the STC, TSF, RISE and cBOSS classifiers from different feature
representations using the CAWPE structure as described in [1]_. The default
implementation differs from the one described in [1]_, in that the STC component
uses the out of bag error (OOB) estimates for weights (described in [2]_) rather
than the cross-validation estimate. OOB is an order of magnitude faster and on
average as good as CV. This means that this version of HIVE COTE is a bit faster
than HC2, although less accurate on average.
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.
tsf_params : dict or None, default=None
Parameters for the TimeSeriesForestClassifier module. If None, uses the default
parameters with n_estimators set to 500.
rise_params : dict or None, default=None
Parameters for the RandomIntervalSpectralForest module. If None, uses the
default parameters with n_estimators set to 500.
cboss_params : dict or None, default=None
Parameters for the ContractableBOSS module. If None, uses the default
parameters.
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.
tsf_weight_ : float
The weight for TSF probabilities.
rise_weight_ : float
The weight for RISE probabilities.
cboss_weight_ : float
The weight for cBOSS probabilities.
See Also
--------
ShapeletTransformClassifier, TimeSeriesForestClassifier,
RandomIntervalSpectralForest, ContractableBOSS
All components of HIVECOTE.
HIVECOTEV2
Successor to HIVECOTEV1.
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] Anthony Bagnall, Michael Flynn, James Large, Jason Lines and
Matthew Middlehurst. "On the usage and performance of the Hierarchical Vote
Collective of Transformation-based Ensembles version 1.0 (hive-cote v1.0)"
International Workshop on Advanced Analytics and Learning on Temporal Data 2020
.. [2] 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:multithreading": True,
"algorithm_type": "hybrid",
}
def __init__(
self,
stc_params=None,
tsf_params=None,
rise_params=None,
cboss_params=None,
verbose=0,
random_state=None,
n_jobs=1,
parallel_backend=None,
):
self.stc_params = stc_params
self.tsf_params = tsf_params
self.rise_params = rise_params
self.cboss_params = cboss_params
self.verbose = verbose
self.random_state = random_state
self.n_jobs = n_jobs
self.parallel_backend = parallel_backend
self.stc_weight_ = 0
self.tsf_weight_ = 0
self.rise_weight_ = 0
self.cboss_weight_ = 0
self._stc_params = stc_params
self._tsf_params = tsf_params
self._rise_params = rise_params
self._cboss_params = cboss_params
self._stc = None
self._tsf = None
self._rise = None
self._cboss = None
super(HIVECOTEV1, self).__init__()
_DEFAULT_N_TREES = 500
_DEFAULT_N_SHAPELETS = 10000
_DEFAULT_N_PARA_SAMPLES = 250
_DEFAULT_MAX_ENSEMBLE_SIZE = 50
def _fit(self, X, y):
"""Fit HIVE-COTE 1.0 to training data.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_channels, n_timepoints]
The training data.
y : array-like, shape = [n_instances]
The class labels.
Returns
-------
self :
Reference to self.
"""
if self.stc_params is None:
self._stc_params = {"n_shapelet_samples": HIVECOTEV1._DEFAULT_N_SHAPELETS}
if self.tsf_params is None:
self._tsf_params = {"n_estimators": HIVECOTEV1._DEFAULT_N_TREES}
if self.rise_params is None:
self._rise_params = {"n_estimators": HIVECOTEV1._DEFAULT_N_TREES}
if self.cboss_params is None:
self._cboss_params = {
"n_parameter_samples": HIVECOTEV1._DEFAULT_N_PARA_SAMPLES,
"max_ensemble_size": HIVECOTEV1._DEFAULT_MAX_ENSEMBLE_SIZE,
}
# Cross-validation size for TSF and RISE
cv_size = 10
_, counts = np.unique(y, return_counts=True)
min_class = max(2, np.min(counts))
if min_class < cv_size:
cv_size = min_class
# 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 TSF
self._tsf = TimeSeriesForestClassifier(
**self._tsf_params,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._tsf.fit(X, y)
if self.verbose > 0:
print("TSF ", datetime.now().strftime("%H:%M:%S %d/%m/%Y")) # noqa
# Find TSF weight using train set estimate found through CV
train_preds = cross_val_predict(
TimeSeriesForestClassifier(
**self._tsf_params, random_state=self.random_state
),
X=X,
y=y,
cv=cv_size,
n_jobs=self._n_jobs,
)
self.tsf_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"TSF train estimate ",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("TSF weight = " + str(self.tsf_weight_)) # noqa
# Build RISE
self._rise = RandomIntervalSpectralEnsembleClassifier(
**self._rise_params,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._rise.fit(X, y)
if self.verbose > 0:
print("RISE ", datetime.now().strftime("%H:%M:%S %d/%m/%Y")) # noqa
# Find RISE weight using train set estimate found through CV
train_preds = cross_val_predict(
RandomIntervalSpectralEnsembleClassifier(
**self._rise_params,
random_state=self.random_state,
),
X=X,
y=y,
cv=cv_size,
n_jobs=self._n_jobs,
)
self.rise_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"RISE train estimate ",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("RISE weight = " + str(self.rise_weight_)) # noqa
# Build cBOSS
self._cboss = ContractableBOSS(
**self._cboss_params,
random_state=self.random_state,
n_jobs=self._n_jobs,
)
self._cboss.fit(X, y)
# Find cBOSS weight using train set estimate
train_probs = self._cboss._get_train_probs(X, y)
train_preds = self._cboss.classes_[np.argmax(train_probs, axis=1)]
self.cboss_weight_ = accuracy_score(y, train_preds) ** 4
if self.verbose > 0:
print( # noqa
"cBOSS (estimate included)",
datetime.now().strftime("%H:%M:%S %d/%m/%Y"),
)
print("cBOSS weight = " + str(self.cboss_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, n_timepoints]
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) -> 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 HC1 probabilities
dists = np.add(
dists,
self._stc.predict_proba(X) * (np.ones(self.n_classes_) * self.stc_weight_),
)
dists = np.add(
dists,
self._tsf.predict_proba(X) * (np.ones(self.n_classes_) * self.tsf_weight_),
)
dists = np.add(
dists,
self._rise.predict_proba(X)
* (np.ones(self.n_classes_) * self.rise_weight_),
)
dists = np.add(
dists,
self._cboss.predict_proba(X)
* (np.ones(self.n_classes_) * self.cboss_weight_),
)
# 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.
HIVECOTEV1 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
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
if parameter_set == "results_comparison":
return {
"stc_params": {
"estimator": RandomForestClassifier(n_estimators=3),
"n_shapelet_samples": 50,
"max_shapelets": 5,
"batch_size": 10,
},
"tsf_params": {"n_estimators": 3},
"rise_params": {"n_estimators": 3},
"cboss_params": {"n_parameter_samples": 5, "max_ensemble_size": 3},
}
else:
return {
"stc_params": {
"estimator": RandomForestClassifier(n_estimators=1),
"n_shapelet_samples": 5,
"max_shapelets": 5,
"batch_size": 5,
},
"tsf_params": {"n_estimators": 1},
"rise_params": {"n_estimators": 1},
"cboss_params": {"n_parameter_samples": 1, "max_ensemble_size": 1},
}