/
base_interval_forest.py
1288 lines (1146 loc) · 55 KB
/
base_interval_forest.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""A base class for interval extracting forest estimators."""
__maintainer__ = []
__all__ = ["BaseIntervalForest"]
import inspect
import time
import warnings
from abc import ABCMeta, abstractmethod
import numpy as np
from joblib import Parallel, delayed
from sklearn.base import BaseEstimator, is_classifier, is_regressor
from sklearn.preprocessing import FunctionTransformer
from sklearn.tree import BaseDecisionTree, DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.utils import check_random_state
from aeon.base._base import _clone_estimator
from aeon.classification.sklearn import ContinuousIntervalTree
from aeon.transformations.base import BaseTransformer
from aeon.transformations.collection.interval_based import (
RandomIntervals,
SupervisedIntervals,
)
from aeon.utils.numba.stats import row_mean, row_slope, row_std
from aeon.utils.validation import check_n_jobs
class BaseIntervalForest(metaclass=ABCMeta):
"""A base class for interval extracting forest estimators.
Allows the implementation of classifiers and regressors along the lines of [1][2][3]
which extract intervals and create an ensemble from the subsequent features.
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.
interval_selection_method : "random", "supervised" or "random-supervised",
default="random"
The interval selection transformer to use.
- "random" uses a RandomIntervalTransformer.
- "supervised" uses a SupervisedIntervalTransformer.
- "random-supervised" uses a SupervisedIntervalTransformer with
randomised elements.
Supervised methods can only be used for classification tasks, and require
function inputs for interval_features rather than transformers.
n_intervals : int, str, list or tuple, default="sqrt"
Number of intervals to extract per tree for each series_transformers series.
An int input will extract that number of intervals from the series, while a str
input will return a function of the series length (may differ per
series_transformers output) to extract that number of intervals.
Valid str inputs are:
- "sqrt": square root of the series length.
- "sqrt-div": sqrt of series length divided by the number
of series_transformers.
A list or tuple of ints and/or strs will extract the number of intervals using
the above rules and sum the results for the final n_intervals. i.e. [4, "sqrt"]
will extract sqrt(n_timepoints) + 4 intervals.
Different number of intervals for each series_transformers series can be
specified using a nested list or tuple. Any list or tuple input containing
another list or tuple must be the same length as the number of
series_transformers.
While random interval extraction will extract the n_intervals intervals total
(removing duplicates), supervised intervals will run the supervised extraction
process n_intervals times, returning more intervals than specified.
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.
interval_features : BaseTransformer, callable, list, tuple, or None, default=None
The features to extract from the intervals using transformers or callable
functions. If None, use the mean, standard deviation, and slope of the series.
Both transformers and functions should be able to take a 2D np.ndarray input.
Functions should output a 1d array (the feature for each series), and
transformers should output a 2d array where rows are the features for each
series. A list or tuple of transformers and/or functions will extract all
features and concatenate the output.
Different features for each series_transformers series can be specified using a
nested list or tuple. Any list or tuple input containing another list or tuple
must be the same length as the number of series_transformers.
series_transformers : BaseTransformer, list, tuple, or None, default=None
The transformers to apply to the series before extracting intervals. If None,
use the series as is.
A list or tuple of transformers will extract intervals from
all transformations concatenate the output. Including None in the list or tuple
will use the series as is for interval extraction.
att_subsample_size : int, float, list, tuple or None, default=None
The number of attributes to subsample for each estimator. If None, use all
If int, use that number of attributes for all estimators. If float, use that
proportion of attributes for all estimators.
Different subsample sizes 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.
replace_nan : "nan", int, float or None, default=None
The value to replace NaNs and infinite values with before fitting the base
estimator. int or float input will replace with the specified value, while
"nan" will replace infinite values with NaNs. If None, do not replace NaNs.
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.
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_cases_ : int
The number of train cases.
n_channels_ : int
The number of channels per case.
n_timepoints_ : int
The length of each series.
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 BaseTransformer
Stores the interval extraction transformer for all estimators.
References
----------
.. [1] H.Deng, G.Runger, E.Tuv and M.Vladimir, "A time series forest for
classification and feature extraction", Information Sciences, 239, 2013
.. [2] Matthew Middlehurst and James Large and Anthony Bagnall. "The Canonical
Interval Forest (CIF) Classifier for Time Series Classification."
IEEE International Conference on Big Data 2020
.. [3] Cabello, Nestor, et al. "Fast and Accurate Time Series Classification
Through Supervised Interval Search." IEEE ICDM 2020
"""
@abstractmethod
def __init__(
self,
base_estimator=None,
n_estimators=200,
interval_selection_method="random",
n_intervals="sqrt",
min_interval_length=3,
max_interval_length=np.inf,
interval_features=None,
series_transformers=None,
att_subsample_size=None,
replace_nan=None,
time_limit_in_minutes=None,
contract_max_n_estimators=500,
random_state=None,
n_jobs=1,
parallel_backend=None,
):
self.base_estimator = base_estimator
self.n_estimators = n_estimators
self.interval_selection_method = interval_selection_method
self.n_intervals = n_intervals
self.min_interval_length = min_interval_length
self.max_interval_length = max_interval_length
self.interval_features = interval_features
self.series_transformers = series_transformers
self.att_subsample_size = att_subsample_size
self.replace_nan = replace_nan
self.time_limit_in_minutes = time_limit_in_minutes
self.contract_max_n_estimators = contract_max_n_estimators
self.random_state = random_state
self.n_jobs = n_jobs
self.parallel_backend = parallel_backend
super().__init__()
# if subsampling attributes, an interval_features transformer must contain a
# parameter name from transformer_feature_selection and an attribute name
# (or property) from transformer_feature_names to allow features to be subsampled
transformer_feature_selection = ["features"]
transformer_feature_names = [
"features_arguments_",
"_features_arguments",
"get_features_arguments",
"_get_features_arguments",
]
# an interval_features transformer must contain one of these attribute names to
# be able to skip transforming features in predict
transformer_feature_skip = ["transform_features_", "_transform_features"]
def _fit(self, X, y):
if getattr(self, "_unit_test_flag", False):
self._transformed_data = self._fit_forest(X, y, save_transformed_data=True)
else:
self._fit_forest(X, y)
return self
def _predict(self, X):
if is_regressor(self):
Xt = self._predict_setup(X)
y_preds = Parallel(
n_jobs=self._n_jobs,
backend=self.parallel_backend,
prefer="threads",
)(
delayed(self._predict_for_estimator)(
Xt,
self.estimators_[i],
self.intervals_[i],
predict_proba=False,
)
for i in range(self._n_estimators)
)
return np.mean(y_preds, axis=0)
else:
return np.array(
[self.classes_[int(np.argmax(prob))] for prob in self._predict_proba(X)]
)
def _predict_proba(self, X):
Xt = self._predict_setup(X)
y_probas = Parallel(
n_jobs=self._n_jobs, backend=self.parallel_backend, prefer="threads"
)(
delayed(self._predict_for_estimator)(
Xt,
self.estimators_[i],
self.intervals_[i],
predict_proba=True,
)
for i in range(self._n_estimators)
)
output = np.sum(y_probas, axis=0) / (
np.ones(self.n_classes_) * self._n_estimators
)
return output
def _fit_predict(self, X, y) -> np.ndarray:
rng = check_random_state(self.random_state)
if is_regressor(self):
Xt = self._fit_forest(X, y, save_transformed_data=True)
p = Parallel(
n_jobs=self._n_jobs, backend=self.parallel_backend, prefer="threads"
)(
delayed(self._train_estimate_for_estimator)(
Xt,
y,
i,
check_random_state(rng.randint(np.iinfo(np.int32).max)),
)
for i in range(self._n_estimators)
)
y_preds, oobs = zip(*p)
results = np.sum(y_preds, axis=0)
divisors = np.zeros(self.n_cases_)
for oob in oobs:
for inst in oob:
divisors[inst] += 1
label_average = np.mean(y)
for i in range(self.n_cases_):
results[i] = (
label_average if divisors[i] == 0 else results[i] / divisors[i]
)
else:
return np.array(
[
self.classes_[int(rng.choice(np.flatnonzero(prob == prob.max())))]
for prob in self._fit_predict_proba(X, y)
]
)
return results
def _fit_predict_proba(self, X, y) -> np.ndarray:
if is_regressor(self):
raise ValueError(
"Train probability estimates are only available for classification"
)
Xt = self._fit_forest(X, y, save_transformed_data=True)
rng = check_random_state(self.random_state)
p = Parallel(
n_jobs=self._n_jobs, backend=self.parallel_backend, prefer="threads"
)(
delayed(self._train_estimate_for_estimator)(
Xt,
y,
i,
check_random_state(rng.randint(np.iinfo(np.int32).max)),
probas=True,
)
for i in range(self._n_estimators)
)
y_probas, oobs = zip(*p)
results = np.sum(y_probas, axis=0)
divisors = np.zeros(self.n_cases_)
for oob in oobs:
for inst in oob:
divisors[inst] += 1
for i in range(self.n_cases_):
results[i] = (
np.ones(self.n_classes_) * (1 / self.n_classes_)
if divisors[i] == 0
else results[i] / (np.ones(self.n_classes_) * divisors[i])
)
return results
def _fit_forest(self, X, y, save_transformed_data=False):
rng = check_random_state(self.random_state)
self.n_cases_, self.n_channels_, self.n_timepoints_ = X.shape
self._base_estimator = self.base_estimator
if self.base_estimator is None:
if is_classifier(self):
self._base_estimator = DecisionTreeClassifier(criterion="entropy")
elif is_regressor(self):
self._base_estimator = DecisionTreeRegressor(criterion="absolute_error")
else:
raise ValueError(
f"{self} must be a scikit-learn compatible classifier or "
"regressor."
)
# base_estimator must be an sklearn estimator
elif not isinstance(self.base_estimator, BaseEstimator):
raise ValueError(
"base_estimator must be a scikit-learn BaseEstimator or None. "
f"Found: {self.base_estimator}"
)
# use the base series if series_transformers is None
if self.series_transformers is None or self.series_transformers == []:
Xt = [X]
self._series_transformers = [None]
# clone series_transformers if it is a transformer and transform the input data
elif _is_transformer(self.series_transformers):
t = _clone_estimator(self.series_transformers, random_state=rng)
Xt = [t.fit_transform(X, y)]
self._series_transformers = [t]
# clone each series_transformers transformer and include the base series if None
# is in the list
elif isinstance(self.series_transformers, (list, tuple)):
Xt = []
self._series_transformers = []
for transformer in self.series_transformers:
if transformer is None:
Xt.append(X)
self._series_transformers.append(None)
elif _is_transformer(transformer):
t = _clone_estimator(transformer, random_state=rng)
Xt.append(t.fit_transform(X, y))
self._series_transformers.append(t)
else:
raise ValueError(
f"Invalid series_transformers list input. Found {transformer}"
)
# other inputs are invalid
else:
raise ValueError(
f"Invalid series_transformers input. Found {self.series_transformers}"
)
# if only a single n_intervals value is passed it must be an int or str
if isinstance(self.n_intervals, (int, str)):
n_intervals = [[self.n_intervals]] * len(Xt)
elif isinstance(self.n_intervals, (list, tuple)):
# if input is a list and only contains ints or strs, use the list for all
# series in Xt
if all(isinstance(item, (int, str)) for item in self.n_intervals):
n_intervals = [self.n_intervals] * len(Xt)
# other lists must be the same length as Xt
elif len(self.n_intervals) != len(Xt):
raise ValueError(
"n_intervals as a list or tuple containing other lists or tuples "
"must be the same length as series_transformers."
)
# list items can be a list of items or a single item for each
# series_transformer, but each individual item must be an int or str
else:
n_intervals = []
for items in self.n_intervals:
if isinstance(items, (list, tuple)):
if not all(isinstance(item, (int, str)) for item in items):
raise ValueError(
"Individual items in a n_intervals list or tuple must "
f"be an int or str. Input {items} does not contain "
"only ints or strs"
)
n_intervals.append(items)
elif isinstance(items, (int, str)):
n_intervals.append([items])
else:
raise ValueError(
"Individual items in a n_intervals list or tuple must be "
f"an int or str. Found: {items}"
)
# other inputs are invalid
else:
raise ValueError(f"Invalid n_intervals input. Found {self.n_intervals}")
# add together the number of intervals for each series_transformer
# str input must be one of a set valid options
self._n_intervals = [0] * len(Xt)
for i, series in enumerate(Xt):
for method in n_intervals[i]:
if isinstance(method, int):
self._n_intervals[i] += method
elif isinstance(method, str):
# sqrt of series length
if method.lower() == "sqrt":
self._n_intervals[i] += int(
np.sqrt(series.shape[2]) * np.sqrt(series.shape[1])
)
# sqrt of series length divided by the number of series_transformers
elif method.lower() == "sqrt-div":
self._n_intervals[i] += int(
(np.sqrt(series.shape[2]) * np.sqrt(series.shape[1]))
/ len(Xt)
)
else:
raise ValueError(
"Invalid str input for n_intervals. Must be "
f'("sqrt","sqrt-div"). Found {method}'
)
# each series_transformer must have at least 1 interval extracted
for i, n in enumerate(self._n_intervals):
if n <= 0:
self._n_intervals[i] = 1
self.total_intervals_ = sum(self._n_intervals)
# minimum interval length
if isinstance(self.min_interval_length, int):
self._min_interval_length = [self.min_interval_length] * len(Xt)
# min_interval_length must be less than one if it is a float (proportion of
# of the series length)
elif (
isinstance(self.min_interval_length, float)
and self.min_interval_length <= 1
):
self._min_interval_length = [
int(self.min_interval_length * t.shape[2]) for t in Xt
]
# if the input is a list, it must be the same length as the number of
# series_transformers
# list values must be ints or floats. The same checks as above are performed
elif isinstance(self.min_interval_length, (list, tuple)):
if len(self.min_interval_length) != len(Xt):
raise ValueError(
"min_interval_length as a list or tuple must be the same length "
"as series_transformers."
)
self._min_interval_length = []
for i, length in enumerate(self.min_interval_length):
if isinstance(length, float) and length <= 1:
self._min_interval_length.append(int(length * Xt[i].shape[2]))
elif isinstance(length, int):
self._min_interval_length.append(length)
else:
raise ValueError(
"min_interval_length list items must be int or floats. "
f"Found {length}"
)
# other inputs are invalid
else:
raise ValueError(
f"Invalid min_interval_length input. Found {self.min_interval_length}"
)
# min_interval_length cannot be less than 3 or greater than the series length
for i, n in enumerate(self._min_interval_length):
if n > Xt[i].shape[2]:
self._min_interval_length[i] = Xt[i].shape[2]
elif n < 3:
self._min_interval_length[i] = 3
# maximum interval length
if (
isinstance(self.max_interval_length, int)
or self.max_interval_length == np.inf
):
self._max_interval_length = [self.max_interval_length] * len(Xt)
# max_interval_length must be at less than one if it is a float (proportion of
# of the series length)
elif (
isinstance(self.max_interval_length, float)
and self.max_interval_length <= 1
):
self._max_interval_length = [
int(self.max_interval_length * t.shape[2]) for t in Xt
]
# if the input is a list, it must be the same length as the number of
# series_transformers
# list values must be ints or floats. The same checks as above are performed
elif isinstance(self.max_interval_length, (list, tuple)):
if len(self.max_interval_length) != len(Xt):
raise ValueError(
"max_interval_length as a list or tuple must be the same length "
"as series_transformers."
)
self._max_interval_length = []
for i, length in enumerate(self.max_interval_length):
if isinstance(length, float) and length <= 1:
self._max_interval_length.append(int(length * Xt[i].shape[2]))
elif isinstance(length, int):
self._max_interval_length.append(length)
else:
raise ValueError(
"max_interval_length list items must be int or floats. "
f"Found {length}"
)
# other inputs are invalid
else:
raise ValueError(
f"Invalid max_interval_length input. Found {self.max_interval_length}"
)
# max_interval_length cannot be less than min_interval_length or greater than
# the series length
for i, n in enumerate(self._max_interval_length):
if n < self._min_interval_length[i]:
self._max_interval_length[i] = self._min_interval_length[i]
elif n > Xt[i].shape[2]:
self._max_interval_length[i] = Xt[i].shape[2]
# we store whether each series_transformer contains a transformer and/or
# function in its interval_features
self._interval_transformer = [False] * len(Xt)
self._interval_function = [False] * len(Xt)
# single transformer or function for all series_transformers
if isinstance(self.interval_features, BaseTransformer):
self._interval_transformer = [True] * len(Xt)
transformer = _clone_estimator(self.interval_features, random_state=rng)
self._interval_features = [[transformer]] * len(Xt)
elif callable(self.interval_features):
self._interval_function = [True] * len(Xt)
self._interval_features = [[self.interval_features]] * len(Xt)
elif isinstance(self.interval_features, (list, tuple)):
# if input is a list and only contains transformers or functions, use the
# list for all series in Xt
if all(
isinstance(item, BaseTransformer) or callable(item)
for item in self.interval_features
):
for feature in self.interval_features:
if isinstance(feature, BaseTransformer):
self._interval_transformer[0] = True
elif callable(feature):
self._interval_function[0] = True
self._interval_features = [self.interval_features] * len(Xt)
# other lists must be the same length as Xt
elif len(self.interval_features) != len(Xt):
raise ValueError(
"interval_features as a list or tuple containing other lists or "
"tuples must be the same length as series_transformers."
)
# list items can be a list of items or a single item for each
# series_transformer, but each individual item must be a transformer
# or function
else:
self._interval_features = []
for i, feature in enumerate(self.interval_features):
if isinstance(feature, (list, tuple)):
for method in feature:
if isinstance(method, BaseTransformer):
self._interval_transformer[i] = True
feature = _clone_estimator(feature, random_state=rng)
elif callable(method):
self._interval_function[i] = True
else:
raise ValueError(
"Individual items in a interval_features list or "
"tuple must be a transformer or function. Input "
f"{feature} does not contain only transformers and "
f"functions."
)
self._interval_features.append(feature)
elif isinstance(feature, BaseTransformer):
self._interval_transformer[i] = True
feature = _clone_estimator(feature, random_state=rng)
self._interval_features.append([feature])
elif callable(feature):
self._interval_function[i] = True
self._interval_features.append([feature])
else:
raise ValueError(
"Individual items in a interval_features list or tuple "
f"must be a transformer or function. Found {feature}"
)
# use basic summary stats by default if None
elif self.interval_features is None:
self._interval_function = [True] * len(Xt)
self._interval_features = [[row_mean, row_std, row_slope]] * len(Xt)
# other inputs are invalid
else:
raise ValueError(
f"Invalid interval_features input. Found {self.interval_features}"
)
# att_subsample_size must be at least one if it is an int
if isinstance(self.att_subsample_size, int):
if self.att_subsample_size < 1:
raise ValueError(
"att_subsample_size must be at least one if it is an int."
)
self._att_subsample_size = [self.att_subsample_size] * len(Xt)
# att_subsample_size must be at less than one if it is a float (proportion of
# total attributed to subsample)
elif isinstance(self.att_subsample_size, float):
if self.att_subsample_size > 1 or self.att_subsample_size <= 0:
raise ValueError(
"att_subsample_size must be between 0 and 1 if it is a float."
)
self._att_subsample_size = [self.att_subsample_size] * len(Xt)
# default is no attribute subsampling with None
elif self.att_subsample_size is None:
self._att_subsample_size = [self.att_subsample_size] * len(Xt)
# if the input is a list, it must be the same length as the number of
# series_transformers
# list values must be ints, floats or None. The same checks as above are
# performed
elif isinstance(self.att_subsample_size, (list, tuple)):
if len(self.att_subsample_size) != len(Xt):
raise ValueError(
"att_subsample_size as a list or tuple must be the same length as "
"series_transformers."
)
self._att_subsample_size = []
for ssize in self.att_subsample_size:
if isinstance(ssize, int):
if ssize < 1:
raise ValueError(
"att_subsample_size in list must be at least one if it is "
"an int."
)
self._att_subsample_size.append(ssize)
elif isinstance(ssize, float):
if ssize > 1:
raise ValueError(
"att_subsample_size in list must be between 0 and 1 if it "
"is a "
"float."
)
self._att_subsample_size.append(ssize)
elif ssize is None:
self._att_subsample_size.append(ssize)
else:
raise ValueError(
"Invalid interval_features input in list. Found "
f"{self.att_subsample_size}"
)
# other inputs are invalid
else:
raise ValueError(
f"Invalid interval_features input. Found {self.att_subsample_size}"
)
# if we are subsampling attributes for a series_transformer and it uses a
# BaseTransformer, we must ensure it has the required parameters and
# attributes to do so
self._transformer_feature_selection = [[]] * len(Xt)
self._transformer_feature_names = [[]] * len(Xt)
for r, att_subsample in enumerate(self._att_subsample_size):
if att_subsample is not None:
for transformer in self._interval_features[r]:
if isinstance(transformer, BaseTransformer):
params = inspect.signature(transformer.__init__).parameters
# the transformer must have a parameter with one of the
# names listed in transformer_feature_selection as a way to
# select which features the transformer should transform
has_params = False
for n in self.transformer_feature_selection:
if params.get(n, None) is not None:
has_params = True
self._transformer_feature_selection[r].append(n)
break
if not has_params:
raise ValueError(
"All transformers in interval_features must have a "
"parameter named in transformer_feature_selection to "
"be used in attribute subsampling."
)
# the transformer must have an attribute with one of the
# names listed in transformer_feature_names as a list or tuple
# of valid options for the previous parameter
has_feature_names = False
for n in self.transformer_feature_names:
if hasattr(transformer, n) and isinstance(
getattr(transformer, n), (list, tuple)
):
has_feature_names = True
self._transformer_feature_names[r].append(n)
break
if not has_feature_names:
raise ValueError(
"All transformers in interval_features must have an "
"attribute or property named in "
"transformer_feature_names to be used in attribute "
"subsampling."
)
# verify the interval_selection_method is a valid string
if isinstance(self.interval_selection_method, str):
# SupervisedIntervals cannot currently handle transformers or regression
if (
self.interval_selection_method.lower() == "supervised"
or self.interval_selection_method.lower() == "random-supervised"
):
if any(self._interval_transformer):
raise ValueError(
"Supervised interval_selection_method must only have function "
"inputs for interval_features."
)
if is_regressor(self):
raise ValueError(
"Supervised interval_selection_method cannot be used for "
"regression."
)
# RandomIntervals
elif not self.interval_selection_method.lower() == "random":
raise ValueError(
'Unknown interval_selection_method, must be one of ("random",'
'"supervised","random-supervised"). '
f"Found: {self.interval_selection_method}"
)
# other inputs are invalid
else:
raise ValueError(
'Unknown interval_selection_method, must be one of ("random",'
'"supervised","random-supervised"). '
f"Found: {self.interval_selection_method}"
)
# verify replace_nan is a valid string, number or None
if (
(not isinstance(self.replace_nan, str) or self.replace_nan.lower() != "nan")
and not isinstance(self.replace_nan, (int, float))
and self.replace_nan is not None
):
raise ValueError(f"Invalid replace_nan input. Found {self.replace_nan}")
self._n_jobs = check_n_jobs(self.n_jobs)
if self.time_limit_in_minutes is not None and self.time_limit_in_minutes > 0:
time_limit = self.time_limit_in_minutes * 60
start_time = time.time()
train_time = 0
self._n_estimators = 0
self.estimators_ = []
self.intervals_ = []
transformed_intervals = []
while (
train_time < time_limit
and self._n_estimators < self.contract_max_n_estimators
):
fit = Parallel(
n_jobs=self._n_jobs,
backend=self.parallel_backend,
prefer="threads",
)(
delayed(self._fit_estimator)(
Xt,
y,
rng.randint(np.iinfo(np.int32).max),
save_transformed_data=save_transformed_data,
)
for _ in range(self._n_jobs)
)
(
estimators,
intervals,
td,
) = zip(*fit)
self.estimators_ += estimators
self.intervals_ += intervals
transformed_intervals += td
self._n_estimators += self._n_jobs
train_time = time.time() - start_time
else:
self._n_estimators = self.n_estimators
fit = Parallel(
n_jobs=self._n_jobs,
backend=self.parallel_backend,
prefer="threads",
)(
delayed(self._fit_estimator)(
Xt,
y,
rng.randint(np.iinfo(np.int32).max),
save_transformed_data=save_transformed_data,
)
for _ in range(self._n_estimators)
)
(
self.estimators_,
self.intervals_,
transformed_intervals,
) = zip(*fit)
return transformed_intervals
def _fit_estimator(self, Xt, y, seed, save_transformed_data=False):
# random state for this estimator
rng = check_random_state(seed)
intervals = []
transform_data_lengths = []
interval_features = np.empty((self.n_cases_, 0))
# for each transformed series
for r in range(len(Xt)):
# subsample attributes if enabled
if self._att_subsample_size[r] is not None:
# separate transformers and functions in separate lists
# add the feature names of transformers to a list to subsample from
# and calculate the total number of features
all_transformers = []
all_transformer_features = []
all_function_features = []
for feature in self._interval_features[r]:
if isinstance(feature, BaseTransformer):
all_transformer_features += getattr(
feature,
self._transformer_feature_names[r][len(all_transformers)],
)
all_transformers.append(feature)
else:
all_function_features.append(feature)
# handle float subsample size
num_features = len(all_transformer_features) + len(
all_function_features
)
att_subsample_size = self._att_subsample_size[r]
if isinstance(self._att_subsample_size[r], float):
att_subsample_size = int(att_subsample_size * num_features)
# if the att_subsample_size is greater than the number of features
# give a warning and add all features
features = []
if att_subsample_size < num_features:
# subsample the transformer and function features by index
atts = rng.choice(
num_features,
att_subsample_size,
replace=False,
)
atts.sort()
# subsample the feature transformers using the
# transformer_feature_names and transformer_feature_selection
# attributes.
# the presence of valid attributes is verified in fit.
count = 0
length = 0
for n, transformer in enumerate(all_transformers):
this_len = len(
getattr(transformer, self._transformer_feature_names[r][n])
)
length += this_len
# subsample feature names from this transformer
t_features = []
while count < len(atts) and atts[count] < length:
t_features.append(
getattr(
transformer,
self._transformer_feature_names[r][n],
)[atts[count] + this_len - length]
)
count += 1
# tell this transformer to only transform the selected features
if len(t_features) > 0:
new_transformer = _clone_estimator(transformer, seed)
setattr(
new_transformer,
self._transformer_feature_selection[r][n],
t_features,
)
features.append(new_transformer)
# subsample the remaining function features
for i in range(att_subsample_size - count):
features.append(all_function_features[atts[count + i] - length])
else:
warnings.warn(
f"Attribute subsample size {att_subsample_size} is larger than "
f"or equal to the number of attributes {num_features} for "
f"series {self._series_transformers[r]}",
stacklevel=2,
)
for feature in self._interval_features[r]:
if isinstance(feature, BaseTransformer):
features.append(_clone_estimator(feature, seed))
else:
features.append(feature)
# add all features while cloning estimators if not subsampling
else:
features = []
for feature in self._interval_features[r]:
if isinstance(feature, BaseTransformer):
features.append(_clone_estimator(feature, seed))
else:
features.append(feature)
# create the selected interval selector and set its parameters
if self.interval_selection_method == "random":
selector = RandomIntervals(
n_intervals=self._n_intervals[r],
min_interval_length=self._min_interval_length[r],
max_interval_length=self._max_interval_length[r],
features=features,
random_state=seed,
)
elif self.interval_selection_method == "supervised":
selector = SupervisedIntervals(
n_intervals=self._n_intervals[r],
min_interval_length=self._min_interval_length[r],
features=features,
randomised_split_point=False,
random_state=seed,
)
elif self.interval_selection_method == "random-supervised":
selector = SupervisedIntervals(
n_intervals=self._n_intervals[r],
min_interval_length=self._min_interval_length[r],
features=features,
randomised_split_point=True,
random_state=seed,
)
# fit the interval selector, transform the current series using it and save
# the transformer
intervals.append(selector)
f = intervals[r].fit_transform(Xt[r], y)