-
Notifications
You must be signed in to change notification settings - Fork 89
/
base.py
380 lines (325 loc) · 14.4 KB
/
base.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
"""
Abstract base class for time series classifiers.
class name: BaseClassifier
Defining methods:
fitting - fit(self, X, y)
predicting - predict(self, X)
- predict_proba(self, X)
Inherited inspection methods:
hyper-parameter inspection - get_params()
fitted parameter inspection - get_fitted_params()
State:
fitted model/strategy - by convention, any attributes ending in "_"
fitted state flag - is_fitted (property)
fitted state inspection - check_is_fitted()
"""
__all__ = [
"BaseClassifier",
]
__author__ = ["mloning", "fkiraly", "TonyBagnall", "MatthewMiddlehurst"]
import time
from abc import ABC, abstractmethod
from typing import final
import numpy as np
import pandas as pd
from sklearn.utils.multiclass import type_of_target
from aeon.base import BaseCollectionEstimator
from aeon.utils.sklearn import is_sklearn_transformer
from aeon.utils.validation._dependencies import _check_estimator_deps
from aeon.utils.validation.collection import get_n_cases
class BaseClassifier(BaseCollectionEstimator, ABC):
"""
Abstract base class for time series classifiers.
Attributes with an underscore suffix are set in the method fit.
Attributes
----------
classes_ : np.ndarray
Class labels, possibly strings.
n_classes_ : integer
Number of classes (length of ``classes_``).
fit_time_ : integer
Time (in milliseconds) for fit to run.
_class_dictionary : dict
Mapping of classes_ onto integers 0...``n_classes_``-1.
_n_jobs : number of threads to use in ``fit`` as determined by ``n_jobs``.
_estimator_type : string required by sklearn, set to "classifier"
"""
_tags = {
"capability:train_estimate": False,
"capability:contractable": False,
}
def __init__(self):
# reserved attributes written to in fit
self.classes_ = [] # classes seen in y, unique labels
self.n_classes_ = 0 # number of unique classes in y
self._class_dictionary = {}
# required for compatibility with some sklearn interfaces e.g.
# CalibratedClassifierCV
self._estimator_type = "classifier"
super(BaseClassifier, self).__init__()
_check_estimator_deps(self)
def __rmul__(self, other):
"""Magic * method, return concatenated ClassifierPipeline, transformers on left.
Overloaded multiplication operation for classifiers. Implemented for ``other``
being a transformer, otherwise returns `NotImplemented`.
Parameters
----------
other: `aeon` transformer, must inherit from BaseTransformer
otherwise, `NotImplemented` is returned
Returns
-------
ClassifierPipeline object, concatenation of `other` (first) with `self` (last).
"""
from aeon.classification.compose import ClassifierPipeline
from aeon.transformations.adapt import TabularToSeriesAdaptor
from aeon.transformations.base import BaseTransformer
from aeon.transformations.compose import TransformerPipeline
# behaviour is implemented only if other inherits from BaseTransformer
# in that case, distinctions arise from whether self or other is a pipeline
if isinstance(other, BaseTransformer):
# ClassifierPipeline already has the dunder method defined
if isinstance(self, ClassifierPipeline):
return other * self
# if other is a TransformerPipeline but self is not, first unwrap it
elif isinstance(other, TransformerPipeline):
return ClassifierPipeline(classifier=self, transformers=other.steps)
# if neither self nor other are a pipeline, construct a ClassifierPipeline
else:
return ClassifierPipeline(classifier=self, transformers=[other])
elif is_sklearn_transformer(other):
return TabularToSeriesAdaptor(other) * self
else:
return NotImplemented
@final
def fit(self, X, y) -> BaseCollectionEstimator:
"""Fit time series classifier to training data.
Parameters
----------
X : 3D np.array
Input data, any number of channels, equal length series of shape ``(
n_instances, n_channels, n_timepoints)``
or 2D np.array (univariate, equal length series) of shape
``(n_instances, n_timepoints)``
or list of numpy arrays (any number of channels, unequal length series)
of shape ``[n_instances]``, 2D np.array ``(n_channels, n_timepoints_i)``,
where ``n_timepoints_i`` is length of series ``i``. Other types are
allowed and converted into one of the above.
y : np.array
shape ``(n_instances)`` - class labels for fitting indices correspond to
instance indices in X.
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.
"""
# reset estimator at the start of fit
self.reset()
# All of this can move up to BaseCollection
start = int(round(time.time() * 1000))
X = self._preprocess_collection(X)
y = self._check_y(y, self.metadata_["n_cases"])
# escape early and do not fit if only one class label has been seen
# in this case, we later predict the single class label seen
if len(self.classes_) == 1:
self.fit_time_ = int(round(time.time() * 1000)) - start
self._is_fitted = True
return self
self._fit(X, y)
self.fit_time_ = int(round(time.time() * 1000)) - start
# this should happen last
self._is_fitted = True
return self
@final
def predict(self, X) -> np.ndarray:
"""Predicts class labels for time series in X.
Parameters
----------
X : 3D np.ndarray
Input data, any number of channels, equal length series of shape ``(
n_instances, n_channels, n_timepoints)``
or 2D np.array (univariate, equal length series) of shape
``(n_instances, n_timepoints)``
or list of numpy arrays (any number of channels, unequal length series)
of shape ``[n_instances]``, 2D np.array ``(n_channels, n_timepoints_i)``,
where ``n_timepoints_i`` is length of series ``i``
other types are allowed and converted into one of the above.
Returns
-------
np.array
shape ``[n_instances]`` - predicted class labels indices correspond to
instance indices in X
"""
self.check_is_fitted()
# handle the single-class-label case
if len(self._class_dictionary) == 1:
n_instances = get_n_cases(X)
return np.repeat(list(self._class_dictionary.keys()), n_instances)
X = self._preprocess_collection(X)
return self._predict(X)
@final
def predict_proba(self, X) -> np.ndarray:
"""Predicts class label probabilities for time series in X.
Parameters
----------
X : 3D np.array
Input data, any number of channels, equal length series of shape ``(
n_instances, n_channels, n_timepoints)``
or 2D np.array (univariate, equal length series) of shape
``(n_instances, n_timepoints)``
or list of numpy arrays (any number of channels, unequal length series)
of shape ``[n_instances]``, 2D np.array ``(n_channels, n_timepoints_i)``,
where ``n_timepoints_i`` is length of series ``i``. other types are
allowed and converted into one of the above.
Returns
-------
np.ndarray
2D array of shape ``(n_cases, n_classes)`` - predicted class probabilities
First dimension indices correspond to instance indices in X,
second dimension indices correspond to class labels, (i, j)-th entry is
estimated probability that i-th instance is of class j
"""
self.check_is_fitted()
# handle the single-class-label case
if len(self._class_dictionary) == 1:
n_instances = get_n_cases(X)
return np.repeat([[1]], n_instances, axis=0)
X = self._preprocess_collection(X)
return self._predict_proba(X)
def score(self, X, y) -> float:
"""Scores predicted labels against ground truth labels on X.
Parameters
----------
X : 3D np.array (any number of channels, equal length series)
of shape (n_instances, n_channels, n_timepoints)
or 2D np.array (univariate, equal length series)
of shape (n_instances, n_timepoints)
or list of numpy arrays (any number of channels, unequal length series)
of shape [n_instances], 2D np.array (n_channels, n_timepoints_i), where
n_timepoints_i is length of series i.
other types are allowed and converted into one of the above.
y : 1D np.ndarray of shape [n_instances] - class labels (ground truth)
indices correspond to instance indices in X.
Returns
-------
float
accuracy score of predict(X) vs y.
"""
from sklearn.metrics import accuracy_score
self.check_is_fitted()
return accuracy_score(y, self.predict(X), normalize=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.
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.
"""
return super().get_test_params(parameter_set=parameter_set)
@abstractmethod
def _fit(self, X, y):
"""Fit time series classifier to training data.
Abstract method, must be implemented.
Parameters
----------
X : guaranteed to be of a type in self.get_tag("X_inner_type")
if self.get_tag("X_inner_type") = "numpy3D":
3D np.ndarray of shape = (n_instances, n_channels, n_timepoints)
if self.get_tag("X_inner_type") = "np-list":
list of 2D np.ndarray of shape = [n_instances]
y : 1D np.array of int, of shape (n_instances,) - class labels for fitting
indices correspond to instance indices in X
Returns
-------
self :
Reference to self.
Notes
-----
Changes state by creating a fitted model that updates attributes
ending in "_".
"""
...
@abstractmethod
def _predict(self, X) -> np.ndarray:
"""Predicts labels for sequences in X.
Abstract method, must be implemented.
Parameters
----------
X : guaranteed to be of a type in self.get_tag("X_inner_type")
if self.get_tag("X_inner_type") = "numpy3D":
3D np.ndarray of shape = (n_instances, n_channels, n_timepoints)
if self.get_tag("X_inner_type") = "np-list":
list of 2D np.ndarray of length = [n_instances]
Returns
-------
y : 1D np.array of int, of shape (n_instances,) - predicted class labels
indices correspond to instance indices in X
"""
...
def _predict_proba(self, X) -> np.ndarray:
"""Predicts labels probabilities for sequences in X.
Default behaviour is to call _predict and set the predicted class probability
to 1, other class probabilities to 0. Override if better estimates are
obtainable.
Parameters
----------
X : guaranteed to be of a type in self.get_tag("X_inner_type")
if self.get_tag("X_inner_type") = "numpy3D":
3D np.ndarray of shape = (n_instances, n_channels, n_timepoints)
if self.get_tag("X_inner_type") = "np-list":
list of 2D np.ndarray of shape = (n_instances,)
Returns
-------
y : 2D array of shape [n_instances, n_classes] - predicted class probabilities
1st dimension indices correspond to instance indices in X
2nd dimension indices correspond to possible labels (integers)
(i, j)-th entry is predictive probability that i-th instance is of class j
"""
preds = self._predict(X)
n_pred = len(preds)
dists = np.zeros((n_pred, self.n_classes_))
for i in range(n_pred):
dists[i, self._class_dictionary[preds[i]]] = 1
return dists
def _check_y(self, y, n_cases):
# Check y valid input for classification task
if not isinstance(y, (pd.Series, np.ndarray)):
raise TypeError(
f"y must be a np.array or a pd.Series, but found type: {type(y)}"
)
if isinstance(y, np.ndarray) and y.ndim > 1:
raise TypeError(f"y must be 1-dimensional, found {y.ndim} dimensions")
# Check matching number of labels
n_labels = y.shape[0]
if n_cases != n_labels:
raise ValueError(
f"Mismatch in number of cases. Number in X = {n_cases} nos in y = "
f"{n_labels}"
)
y_type = type_of_target(y)
if y_type != "binary" and y_type != "multiclass":
raise ValueError(
f"y type is {y_type} which is not valid for classification. "
f"Should be binary or multiclass according to type_of_target"
)
if isinstance(y, pd.Series):
y = pd.Series.to_numpy(y)
# remember class labels
self.classes_ = np.unique(y)
self.n_classes_ = self.classes_.shape[0]
self._class_dictionary = {}
for index, class_val in enumerate(self.classes_):
self._class_dictionary[class_val] = index
return y