-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
_stc.py
376 lines (320 loc) · 14.5 KB
/
_stc.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
"""A shapelet transform classifier (STC).
Shapelet transform classifier pipeline that simply performs a (configurable) shapelet
transform then builds (by default) a rotation forest classifier on the output.
"""
__author__ = ["TonyBagnall", "MatthewMiddlehurst"]
__all__ = ["ShapeletTransformClassifier"]
import numpy as np
from sklearn.model_selection import cross_val_predict
from sktime.base._base import _clone_estimator
from sktime.classification.base import BaseClassifier
from sktime.classification.sklearn import RotationForest
from sktime.transformations.panel.shapelet_transform import RandomShapeletTransform
from sktime.utils.validation.panel import check_X_y
class ShapeletTransformClassifier(BaseClassifier):
"""A shapelet transform classifier (STC).
Implementation of the binary shapelet transform classifier pipeline along the lines
of [1][2] but with random shapelet sampling. Transforms the data using the
configurable ``RandomShapeletTransform`` and then builds a ``RotationForest``
classifier.
As some implementations and applications contract the transformation solely,
contracting is available for the transform only and both classifier and transform.
Parameters
----------
n_shapelet_samples : int, default=10000
The number of candidate shapelets to be considered for the final transform.
Filtered down to ``<= max_shapelets``, keeping the shapelets with the most
information gain.
max_shapelets : int or None, default=None
Max number of shapelets to keep for the final transform. Each class value will
have its own max, set to ``n_classes_ / max_shapelets``. If ``None``, uses the
minimum between ``10 * n_instances_`` and ``1000``.
max_shapelet_length : int or None, default=None
Lower bound on candidate shapelet lengths for the transform. If ``None``, no
max length is used
estimator : BaseEstimator or None, default=None
Base estimator for the ensemble, can be supplied a sklearn ``BaseEstimator``. If
``None`` a default ``RotationForest`` classifier is used.
transform_limit_in_minutes : int, default=0
Time contract to limit transform time in minutes for the shapelet transform,
overriding ``n_shapelet_samples``. A value of ``0`` means ``n_shapelet_samples``
is used.
time_limit_in_minutes : int, default=0
Time contract to limit build time in minutes, overriding ``n_shapelet_samples``
and ``transform_limit_in_minutes``. The ``estimator`` will only be contracted if
a ``time_limit_in_minutes parameter`` is present. Default of ``0`` means
``n_shapelet_samples`` or ``transform_limit_in_minutes`` is used.
contract_max_n_shapelet_samples : int, default=np.inf
Max number of shapelets to extract when contracting the transform with
``transform_limit_in_minutes`` or ``time_limit_in_minutes``.
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.
batch_size : int or None, default=100
Number of shapelet candidates processed before being merged into the set of best
shapelets in the transform.
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.
fit_time_ : int
The time (in milliseconds) for ``fit`` to run.
n_instances_ : int
The number of train cases in the training set.
n_dims_ : int
The number of dimensions per case in the training set.
series_length_ : int
The length of each series 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``.
See Also
--------
RandomShapeletTransform : The randomly sampled shapelet transform.
RotationForest : The default rotation forest classifier used.
Notes
-----
For the Java version, see
`tsml <https://github.com/uea-machine-learning/tsml/blob/master/src/main/
java/tsml/classifiers/shapelet_based/ShapeletTransformClassifier.java>`_.
References
----------
.. [1] Jon Hills et al., "Classification of time series by shapelet transformation",
Data Mining and Knowledge Discovery, 28(4), 851--881, 2014.
.. [2] A. Bostrom and A. Bagnall, "Binary Shapelet Transform for Multiclass Time
Series Classification", Transactions on Large-Scale Data and Knowledge Centered
Systems, 32, 2017.
Examples
--------
>>> from sktime.classification.shapelet_based import ShapeletTransformClassifier
>>> from sktime.classification.sklearn import RotationForest
>>> from sktime.datasets import load_unit_test
>>> X_train, y_train = load_unit_test(split="train", return_X_y=True)
>>> X_test, y_test = load_unit_test(split="test", return_X_y=True) # doctest: +SKIP
>>> clf = ShapeletTransformClassifier(
... estimator=RotationForest(n_estimators=3),
... n_shapelet_samples=100,
... max_shapelets=10,
... batch_size=20,
... ) # doctest: +SKIP
>>> clf.fit(X_train, y_train) # doctest: +SKIP
ShapeletTransformClassifier(...)
>>> y_pred = clf.predict(X_test) # doctest: +SKIP
"""
_tags = {
# packaging info
# --------------
"authors": ["TonyBagnall", "MatthewMiddlehurst"],
"python_dependencies": "numba",
# estimator type
# --------------
"capability:multivariate": True,
"capability:train_estimate": True,
"capability:contractable": True,
"capability:multithreading": True,
"capability:predict_proba": True,
"classifier_type": "shapelet",
}
def __init__(
self,
n_shapelet_samples=10000,
max_shapelets=None,
max_shapelet_length=None,
estimator=None,
transform_limit_in_minutes=0,
time_limit_in_minutes=0,
contract_max_n_shapelet_samples=np.inf,
save_transformed_data=False,
n_jobs=1,
batch_size=100,
random_state=None,
):
self.n_shapelet_samples = n_shapelet_samples
self.max_shapelets = max_shapelets
self.max_shapelet_length = max_shapelet_length
self.estimator = estimator
self.transform_limit_in_minutes = transform_limit_in_minutes
self.time_limit_in_minutes = time_limit_in_minutes
self.contract_max_n_shapelet_samples = contract_max_n_shapelet_samples
self.save_transformed_data = save_transformed_data
self.random_state = random_state
self.batch_size = batch_size
self.n_jobs = n_jobs
self.n_instances_ = 0
self.n_dims_ = 0
self.series_length_ = 0
self.transformed_data_ = []
self._transformer = None
self._estimator = estimator
self._transform_limit_in_minutes = 0
self._classifier_limit_in_minutes = 0
super().__init__()
def _fit(self, X, y):
"""Fit ShapeletTransformClassifier to training data.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_dimensions, 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 "_".
"""
self.n_instances_, self.n_dims_, self.series_length_ = X.shape
if self.time_limit_in_minutes > 0:
# contracting 2/3 transform (with 1/5 of that taken away for final
# transform), 1/3 classifier
third = self.time_limit_in_minutes / 3
self._classifier_limit_in_minutes = third
self._transform_limit_in_minutes = (third * 2) / 5 * 4
elif self.transform_limit_in_minutes > 0:
self._transform_limit_in_minutes = self.transform_limit_in_minutes
self._transformer = RandomShapeletTransform(
n_shapelet_samples=self.n_shapelet_samples,
max_shapelets=self.max_shapelets,
max_shapelet_length=self.max_shapelet_length,
time_limit_in_minutes=self._transform_limit_in_minutes,
contract_max_n_shapelet_samples=self.contract_max_n_shapelet_samples,
n_jobs=self.n_jobs,
batch_size=self.batch_size,
random_state=self.random_state,
)
self._estimator = _clone_estimator(
RotationForest() if self.estimator is None else self.estimator,
self.random_state,
)
if isinstance(self._estimator, RotationForest):
self._estimator.save_transformed_data = self.save_transformed_data
m = getattr(self._estimator, "n_jobs", None)
if m is not None:
self._estimator.n_jobs = self._threads_to_use
m = getattr(self._estimator, "time_limit_in_minutes", None)
if m is not None and self.time_limit_in_minutes > 0:
self._estimator.time_limit_in_minutes = self._classifier_limit_in_minutes
X_t = self._transformer.fit_transform(X, y).to_numpy()
if self.save_transformed_data:
self.transformed_data_ = X_t
self._estimator.fit(X_t, y)
return self
def _predict(self, X) -> np.ndarray:
"""Predicts labels for sequences in X.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_dimensions, series_length]
The data to make predictions for.
Returns
-------
y : array-like, shape = [n_instances]
Predicted class labels.
"""
X_t = self._transformer.transform(X).to_numpy()
return self._estimator.predict(X_t)
def _predict_proba(self, X) -> np.ndarray:
"""Predicts labels probabilities for sequences in X.
Parameters
----------
X : 3D np.array of shape = [n_instances, n_dimensions, 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_.
"""
X_t = self._transformer.transform(X).to_numpy()
m = getattr(self._estimator, "predict_proba", None)
if callable(m):
return self._estimator.predict_proba(X_t)
else:
dists = np.zeros((X.shape[0], self.n_classes_))
preds = self._estimator.predict(X_t)
for i in range(0, X.shape[0]):
dists[i, np.where(self.classes_ == preds[i])] = 1
return dists
def _get_train_probs(self, X, y) -> np.ndarray:
from sktime.datatypes import convert_to
self.check_is_fitted()
if not isinstance(X, np.ndarray):
X = convert_to(X, "numpy3D")
X, y = check_X_y(X, y, coerce_to_pandas=True)
n_instances, n_dims = X.shape
if n_instances != self.n_instances_ or n_dims != self.n_dims_:
raise ValueError(
"n_instances, n_dims 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.")
if isinstance(self.estimator, RotationForest) or self.estimator is None:
return self._estimator._get_train_probs(self.transformed_data_, y)
else:
m = getattr(self._estimator, "predict_proba", None)
if not callable(m):
raise ValueError("Estimator must have a predict_proba method.")
cv_size = 10
_, counts = np.unique(y, return_counts=True)
min_class = np.min(counts)
if min_class < cv_size:
cv_size = min_class
estimator = _clone_estimator(self.estimator, self.random_state)
return cross_val_predict(
estimator,
X=self.transformed_data_,
y=y,
cv=cv_size,
method="predict_proba",
n_jobs=self._threads_to_use,
)
@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.
For classifiers, a "default" set of parameters should be provided for
general testing, and a "results_comparison" set for comparing against
previously recorded results if the general set does not produce suitable
probabilities to compare against.
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 {
"estimator": RandomForestClassifier(n_estimators=5),
"n_shapelet_samples": 50,
"max_shapelets": 10,
"batch_size": 10,
}
else:
return {
"estimator": RotationForest(n_estimators=2),
"n_shapelet_samples": 10,
"max_shapelets": 3,
"batch_size": 5,
"save_transformed_data": True,
}