-
Notifications
You must be signed in to change notification settings - Fork 89
/
_tsfresh_classifier.py
248 lines (210 loc) · 8.45 KB
/
_tsfresh_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
"""TSFresh Classifier.
Pipeline classifier using the TSFresh transformer and an estimator.
"""
__author__ = ["MatthewMiddlehurst"]
__all__ = ["TSFreshClassifier"]
import warnings
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from aeon.base._base import _clone_estimator
from aeon.classification.base import BaseClassifier
from aeon.transformations.collection.feature_based import (
TSFreshFeatureExtractor,
TSFreshRelevantFeatureExtractor,
)
class TSFreshClassifier(BaseClassifier):
"""
Time Series Feature Extraction based on Scalable Hypothesis Tests classifier.
This classifier simply transforms the input data using the TSFresh [1]_
transformer and builds a provided estimator using the transformed data.
Parameters
----------
default_fc_parameters : str, default="efficient"
Set of TSFresh features to be extracted, options are "minimal", "efficient" or
"comprehensive".
relevant_feature_extractor : bool, default=False
Remove irrelevant features using the FRESH algorithm.
estimator : sklearn classifier, default=None
An sklearn estimator to be built using the transformed data. Defaults to a
Random Forest with 200 trees.
verbose : int, default=0
Level of output printed to the console (for information only).
n_jobs : int, default=1
The number of jobs to run in parallel for both `fit` and `predict`.
``-1`` means using all processors.
chunksize : int or None, default=None
Number of series processed in each parallel TSFresh job, should be optimised
for efficient parallelisation.
random_state : int or None, default=None
Seed for random, integer.
Attributes
----------
n_classes_ : int
Number of classes. Extracted from the data.
classes_ : ndarray of shape (n_classes_)
Holds the label for each class.
See Also
--------
TSFreshFeatureExtractor, TSFreshRelevantFeatureExtractor
References
----------
.. [1] Christ, Maximilian, et al. "Time series feature extraction on basis of
scalable hypothesis tests (tsfresh–a python package)." Neurocomputing 307
(2018): 72-77.
https://www.sciencedirect.com/science/article/pii/S0925231218304843
"""
_tags = {
"capability:multivariate": True,
"capability:multithreading": True,
"algorithm_type": "feature",
"python_dependencies": "tsfresh",
}
def __init__(
self,
default_fc_parameters="efficient",
relevant_feature_extractor=True,
estimator=None,
verbose=0,
n_jobs=1,
chunksize=None,
random_state=None,
):
self.default_fc_parameters = default_fc_parameters
self.relevant_feature_extractor = relevant_feature_extractor
self.estimator = estimator
self.verbose = verbose
self.n_jobs = n_jobs
self.chunksize = chunksize
self.random_state = random_state
self._transformer = None
self._estimator = None
self._return_majority_class = False
self._majority_class = 0
super(TSFreshClassifier, self).__init__()
def _fit(self, X, y):
"""Fit a pipeline on cases (X,y), where y is the target variable.
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.
"""
self._transformer = (
TSFreshRelevantFeatureExtractor(
default_fc_parameters=self.default_fc_parameters,
n_jobs=self._n_jobs,
chunksize=self.chunksize,
)
if self.relevant_feature_extractor
else TSFreshFeatureExtractor(
default_fc_parameters=self.default_fc_parameters,
n_jobs=self._n_jobs,
chunksize=self.chunksize,
)
)
self._estimator = _clone_estimator(
RandomForestClassifier(n_estimators=200)
if self.estimator is None
else self.estimator,
self.random_state,
)
if self.verbose < 2:
self._transformer.show_warnings = False
if self.verbose < 1:
self._transformer.disable_progressbar = True
m = getattr(self._estimator, "n_jobs", None)
if m is not None:
self._estimator.n_jobs = self._n_jobs
X_t = self._transformer.fit_transform(X, y)
if X_t.shape[1] == 0:
warnings.warn(
"TSFresh has extracted no features from the data. Returning the "
"majority class in predictions. Setting "
"relevant_feature_extractor=False will keep all features.",
UserWarning,
stacklevel=2,
)
self._return_majority_class = True
self._majority_class = np.argmax(np.unique(y, return_counts=True)[1])
else:
self._estimator.fit(X_t, y)
return self
def _predict(self, X) -> np.ndarray:
"""Predict class values of n instances 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.
"""
if self._return_majority_class:
return np.full(X.shape[0], self.classes_[self._majority_class])
return self._estimator.predict(self._transformer.transform(X))
def _predict_proba(self, X) -> np.ndarray:
"""Predict class probabilities for n instances 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_.
"""
if self._return_majority_class:
dists = np.zeros((X.shape[0], self.n_classes_))
dists[:, self._majority_class] = 1
return dists
m = getattr(self._estimator, "predict_proba", None)
if callable(m):
return self._estimator.predict_proba(self._transformer.transform(X))
else:
dists = np.zeros((X.shape[0], self.n_classes_))
preds = self._estimator.predict(self._transformer.transform(X))
for i in range(0, X.shape[0]):
dists[i, self._class_dictionary[preds[i]]] = 1
return dists
@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.
TSFreshClassifier 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`.
"""
if parameter_set == "results_comparison":
return {
"estimator": RandomForestClassifier(n_estimators=10),
"default_fc_parameters": "minimal",
"relevant_feature_extractor": False,
}
else:
return {
"estimator": RandomForestClassifier(n_estimators=2),
"default_fc_parameters": "minimal",
"relevant_feature_extractor": False,
}