-
Notifications
You must be signed in to change notification settings - Fork 87
/
_rise.py
254 lines (224 loc) · 10.4 KB
/
_rise.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
"""Random Interval Spectral Ensemble (RISE) classifier."""
__author__ = ["TonyBagnall", "MatthewMiddlehurst"]
__all__ = ["RandomIntervalSpectralEnsembleClassifier"]
import numpy as np
from aeon.base.estimator.interval_based.base_interval_forest import BaseIntervalForest
from aeon.classification import BaseClassifier
from aeon.classification.sklearn import ContinuousIntervalTree
from aeon.transformations.collection import (
AutocorrelationFunctionTransformer,
PeriodogramTransformer,
)
class RandomIntervalSpectralEnsembleClassifier(BaseIntervalForest, BaseClassifier):
"""
Random Interval Spectral Ensemble (RISE) classifier.
Input: n series length m
For each tree
- sample a random intervals
- take the ACF and PS over this interval, and concatenate features
- build a tree on new features
Ensemble the trees through averaging probabilities.
Parameters
----------
base_estimator : BaseEstimator or None, default=None
scikit-learn BaseEstimator used to build the interval ensemble. If None, use a
simple decision tree.
n_estimators : int, default=200
Number of estimators to build for the ensemble.
min_interval_length : int, float, list, or tuple, default=3
Minimum length of intervals to extract from series. float inputs take a
proportion of the series length to use as the minimum interval length.
Different minimum interval lengths for each series_transformers series can be
specified using a list or tuple. Any list or tuple input must be the same length
as the number of series_transformers.
max_interval_length : int, float, list, or tuple, default=np.inf
Maximum length of intervals to extract from series. float inputs take a
proportion of the series length to use as the maximum interval length.
Different maximum interval lengths for each series_transformers series can be
specified using a list or tuple. Any list or tuple input must be the same length
as the number of series_transformers.
Ignored for supervised interval_selection_method inputs.
acf_lag : int or callable, default=100
The maximum number of autocorrelation terms to use. If callable, the function
should take a 3D numpy array of shape (n_instances, n_channels, n_timepoints)
and return an integer.
acf_min_values : int, default=0
Never use fewer than this number of terms to find a correlation unless the
series length is too short. This will reduce n_lags if needed.
time_limit_in_minutes : int, default=0
Time contract to limit build time in minutes, overriding n_estimators.
Default of 0 means n_estimators are used.
contract_max_n_estimators : int, default=500
Max number of estimators when time_limit_in_minutes is set.
use_pyfftw : bool, default=False
Whether to use the pyfftw library for FFT calculations. Requires the pyfftw
package to be installed.
save_transformed_data : bool, default="deprecated"
Save the data transformed in ``fit``.
Deprecated and will be removed in v0.8.0. Use ``fit_predict`` and
``fit_predict_proba`` to generate train estimates instead.
``transformed_data_`` will also be removed.
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, 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_instances_ : int
The number of train cases in the training set.
n_channels_ : int
The number of dimensions per case in the training set.
n_timepoints_ : int
The length of each series in the training set.
n_classes_ : int
Number of classes. Extracted from the data.
classes_ : ndarray of shape (n_classes_)
Holds the label for each class.
total_intervals_ : int
Total number of intervals per tree from all representations.
estimators_ : list of shape (n_estimators) of BaseEstimator
The collections of estimators trained in fit.
intervals_ : list of shape (n_estimators) of TransformerMixin
Stores the interval extraction transformer for all estimators.
transformed_data_ : list of shape (n_estimators) of ndarray with shape
(n_instances_ ,total_intervals * att_subsample_size)
The transformed dataset for all estimators. Only saved when
save_transformed_data is true.
See Also
--------
RandomIntervalSpectralEnsembleRegressor
Notes
-----
For the Java version, see
`TSML <https://github.com/uea-machine-learning/tsml/blob/master/src/main/java/tsml/
classifiers/interval_based/RISE.java>`_.
References
----------
.. [1] Jason Lines, Sarah Taylor and Anthony Bagnall, "Time Series Classification
with HIVE-COTE: The Hierarchical Vote Collective of Transformation-Based
Ensembles", ACM Transactions on Knowledge and Data Engineering, 12(5): 2018
Examples
--------
>>> from aeon.classification.interval_based import (
... RandomIntervalSpectralEnsembleClassifier
... )
>>> from aeon.testing.utils.data_gen import make_example_3d_numpy
>>> X, y = make_example_3d_numpy(n_cases=10, n_channels=1, n_timepoints=12,
... return_y=True, random_state=0)
>>> clf = RandomIntervalSpectralEnsembleClassifier(n_estimators=10, random_state=0)
>>> clf.fit(X, y)
RandomIntervalSpectralEnsembleClassifier(n_estimators=10, random_state=0)
>>> clf.predict(X)
array([0, 1, 0, 1, 0, 0, 1, 1, 1, 0])
"""
_tags = {
"capability:multivariate": True,
"capability:train_estimate": True,
"capability:contractable": True,
"capability:multithreading": True,
"algorithm_type": "interval",
}
def __init__(
self,
base_estimator=None,
n_estimators=200,
min_interval_length=3,
max_interval_length=np.inf,
acf_lag=100,
acf_min_values=4,
time_limit_in_minutes=None,
contract_max_n_estimators=500,
use_pyfftw=False,
save_transformed_data="deprecated",
random_state=None,
n_jobs=1,
parallel_backend=None,
):
self.acf_lag = acf_lag
self.acf_min_values = acf_min_values
self.use_pyfftw = use_pyfftw
if use_pyfftw:
self.set_tags(**{"python_dependencies": "pyfftw"})
if isinstance(base_estimator, ContinuousIntervalTree):
replace_nan = "nan"
else:
replace_nan = 0
interval_features = [
PeriodogramTransformer(use_pyfftw=use_pyfftw, pad_with="mean"),
AutocorrelationFunctionTransformer(
n_lags=acf_lag, min_values=acf_min_values
),
]
super().__init__(
base_estimator=base_estimator,
n_estimators=n_estimators,
interval_selection_method="random",
n_intervals=1,
min_interval_length=min_interval_length,
max_interval_length=max_interval_length,
interval_features=interval_features,
series_transformers=None,
att_subsample_size=None,
replace_nan=replace_nan,
time_limit_in_minutes=time_limit_in_minutes,
contract_max_n_estimators=contract_max_n_estimators,
save_transformed_data=save_transformed_data,
random_state=random_state,
n_jobs=n_jobs,
parallel_backend=parallel_backend,
)
def _fit(self, X, y):
return super()._fit(X, y)
def _predict(self, X) -> np.ndarray:
return super()._predict(X)
def _predict_proba(self, X) -> np.ndarray:
return super()._predict_proba(X)
def _fit_predict(self, X, y) -> np.ndarray:
return super()._fit_predict(X, y)
def _fit_predict_proba(self, X, y) -> np.ndarray:
return super()._fit_predict_proba(X, y)
@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.
RandomIntervalSpectralEnsembleClassifier 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
"train_estimate" - used in some classifiers that set the
"capability:train_estimate" tag to True to allow for more efficient
testing when relevant parameters are available
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 {"n_estimators": 10}
elif parameter_set == "contracting":
return {
"time_limit_in_minutes": 5,
"contract_max_n_estimators": 2,
}
else:
return {"n_estimators": 2}