1
1
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/tsfeatures.ipynb.
2
2
3
3
# %% auto 0
4
- __all__ = ['FREQS' , ' acf_features' , 'arch_stat' , 'count_entropy' , 'crossing_points' , 'entropy' , 'flat_spots' , 'frequency' ,
5
- 'guerrero' , ' heterogeneity' , 'holt_parameters' , 'hurst' , 'hw_parameters' , 'intervals' , 'lumpiness' ,
6
- 'nonlinearity' , ' pacf_features' , 'series_length' , 'sparsity' , 'stability' , 'stl_features' , 'unitroot_kpss' ,
7
- 'unitroot_pp' , ' tsfeatures' ]
4
+ __all__ = ['acf_features' , 'arch_stat' , 'count_entropy' , 'crossing_points' , 'entropy' , 'flat_spots' , 'frequency' , 'guerrero ' ,
5
+ 'heterogeneity' , 'holt_parameters' , 'hurst' , 'hw_parameters' , 'intervals' , 'lumpiness' , 'nonlinearity ' ,
6
+ 'pacf_features' , 'series_length' , 'sparsity' , 'stability' , 'stl_features' , 'unitroot_kpss' , 'unitroot_pp ' ,
7
+ 'tsfeatures' ]
8
8
9
9
# %% ../nbs/tsfeatures.ipynb 3
10
10
import os
45
45
from .utils import *
46
46
47
47
# %% ../nbs/tsfeatures.ipynb 7
48
- FREQS = {"H" : 24 , "D" : 1 , "M" : 12 , "Q" : 4 , "W" : 1 , "Y" : 1 }
49
-
50
- # %% ../nbs/tsfeatures.ipynb 8
51
48
def acf_features (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
52
49
"""Calculates autocorrelation function features.
53
50
@@ -87,7 +84,12 @@ def acf_features(x: np.array, freq: int = 1) -> Dict[str, float]:
87
84
else :
88
85
acfdiff2x = [np .nan ] * 2
89
86
# first autocorrelation coefficient
90
- acf_1 = acfx [1 ]
87
+
88
+ try :
89
+ acf_1 = acfx [1 ]
90
+ except :
91
+ acf_1 = np .nan
92
+
91
93
# sum of squares of first 10 autocorrelation coefficients
92
94
sum_of_sq_acf10 = np .sum ((acfx [1 :11 ]) ** 2 ) if size_x > 10 else np .nan
93
95
# first autocorrelation ciefficient of differenced series
@@ -113,7 +115,7 @@ def acf_features(x: np.array, freq: int = 1) -> Dict[str, float]:
113
115
114
116
return output
115
117
116
- # %% ../nbs/tsfeatures.ipynb 11
118
+ # %% ../nbs/tsfeatures.ipynb 10
117
119
def arch_stat (
118
120
x : np .array , freq : int = 1 , lags : int = 12 , demean : bool = True
119
121
) -> Dict [str , float ]:
@@ -134,7 +136,7 @@ def arch_stat(
134
136
if len (x ) <= lags + 1 :
135
137
return {"arch_lm" : np .nan }
136
138
if demean :
137
- x -= np .mean (x )
139
+ x = x - np .mean (x )
138
140
139
141
size_x = len (x )
140
142
mat = embed (x ** 2 , lags + 1 )
@@ -148,7 +150,7 @@ def arch_stat(
148
150
149
151
return {"arch_lm" : r_squared }
150
152
151
- # %% ../nbs/tsfeatures.ipynb 14
153
+ # %% ../nbs/tsfeatures.ipynb 13
152
154
def count_entropy (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
153
155
"""Count entropy.
154
156
@@ -169,7 +171,7 @@ def count_entropy(x: np.array, freq: int = 1) -> Dict[str, float]:
169
171
170
172
return {"count_entropy" : entropy }
171
173
172
- # %% ../nbs/tsfeatures.ipynb 15
174
+ # %% ../nbs/tsfeatures.ipynb 14
173
175
def crossing_points (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
174
176
"""Crossing points.
175
177
@@ -194,7 +196,7 @@ def crossing_points(x: np.array, freq: int = 1) -> Dict[str, float]:
194
196
195
197
return {"crossing_points" : cross .sum ()}
196
198
197
- # %% ../nbs/tsfeatures.ipynb 16
199
+ # %% ../nbs/tsfeatures.ipynb 15
198
200
def entropy (x : np .array , freq : int = 1 , base : float = e ) -> Dict [str , float ]:
199
201
"""Calculates sample entropy.
200
202
@@ -218,7 +220,7 @@ def entropy(x: np.array, freq: int = 1, base: float = e) -> Dict[str, float]:
218
220
219
221
return {"entropy" : entropy }
220
222
221
- # %% ../nbs/tsfeatures.ipynb 17
223
+ # %% ../nbs/tsfeatures.ipynb 16
222
224
def flat_spots (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
223
225
"""Flat spots.
224
226
@@ -240,8 +242,9 @@ def flat_spots(x: np.array, freq: int = 1) -> Dict[str, float]:
240
242
return {"flat_spots" : np .nan }
241
243
242
244
rlex = np .array ([sum (1 for i in g ) for k , g in groupby (cutx )]).max ()
245
+ return {"flat_spots" : rlex }
243
246
244
- # %% ../nbs/tsfeatures.ipynb 18
247
+ # %% ../nbs/tsfeatures.ipynb 17
245
248
def frequency (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
246
249
"""Frequency.
247
250
@@ -260,7 +263,7 @@ def frequency(x: np.array, freq: int = 1) -> Dict[str, float]:
260
263
261
264
return {"frequency" : freq }
262
265
263
- # %% ../nbs/tsfeatures.ipynb 19
266
+ # %% ../nbs/tsfeatures.ipynb 18
264
267
def guerrero (
265
268
x : np .array , freq : int = 1 , lower : int = - 1 , upper : int = 2
266
269
) -> Dict [str , float ]:
@@ -295,7 +298,7 @@ def guerrero(
295
298
296
299
return {"guerrero" : min_ }
297
300
298
- # %% ../nbs/tsfeatures.ipynb 20
301
+ # %% ../nbs/tsfeatures.ipynb 19
299
302
def heterogeneity (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
300
303
"""Heterogeneity.
301
304
@@ -358,7 +361,7 @@ def heterogeneity(x: np.array, freq: int = 1) -> Dict[str, float]:
358
361
359
362
return output
360
363
361
- # %% ../nbs/tsfeatures.ipynb 21
364
+ # %% ../nbs/tsfeatures.ipynb 20
362
365
def holt_parameters (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
363
366
"""Fitted parameters of a Holt model.
364
367
@@ -386,7 +389,7 @@ def holt_parameters(x: np.array, freq: int = 1) -> Dict[str, float]:
386
389
387
390
return params
388
391
389
- # %% ../nbs/tsfeatures.ipynb 22
392
+ # %% ../nbs/tsfeatures.ipynb 21
390
393
def hurst (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
391
394
"""Hurst index.
392
395
@@ -409,7 +412,7 @@ def hurst(x: np.array, freq: int = 1) -> Dict[str, float]:
409
412
410
413
return {"hurst" : hurst_index }
411
414
412
- # %% ../nbs/tsfeatures.ipynb 23
415
+ # %% ../nbs/tsfeatures.ipynb 22
413
416
def hw_parameters (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
414
417
"""Fitted parameters of a Holt-Winters model.
415
418
@@ -441,7 +444,7 @@ def hw_parameters(x: np.array, freq: int = 1) -> Dict[str, float]:
441
444
442
445
return params
443
446
444
- # %% ../nbs/tsfeatures.ipynb 24
447
+ # %% ../nbs/tsfeatures.ipynb 23
445
448
def intervals (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
446
449
"""Intervals with demand.
447
450
@@ -465,7 +468,7 @@ def intervals(x: np.array, freq: int = 1) -> Dict[str, float]:
465
468
466
469
return {"intervals_mean" : np .mean (y ), "intervals_sd" : np .std (y , ddof = 1 )}
467
470
468
- # %% ../nbs/tsfeatures.ipynb 25
471
+ # %% ../nbs/tsfeatures.ipynb 24
469
472
def lumpiness (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
470
473
"""lumpiness.
471
474
@@ -499,7 +502,7 @@ def lumpiness(x: np.array, freq: int = 1) -> Dict[str, float]:
499
502
500
503
return {"lumpiness" : lumpiness }
501
504
502
- # %% ../nbs/tsfeatures.ipynb 26
505
+ # %% ../nbs/tsfeatures.ipynb 25
503
506
def nonlinearity (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
504
507
"""Nonlinearity.
505
508
@@ -524,7 +527,7 @@ def nonlinearity(x: np.array, freq: int = 1) -> Dict[str, float]:
524
527
525
528
return {"nonlinearity" : test }
526
529
527
- # %% ../nbs/tsfeatures.ipynb 27
530
+ # %% ../nbs/tsfeatures.ipynb 26
528
531
def pacf_features (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
529
532
"""Calculates partial autocorrelation function features.
530
533
@@ -595,7 +598,7 @@ def pacf_features(x: np.array, freq: int = 1) -> Dict[str, float]:
595
598
596
599
return output
597
600
598
- # %% ../nbs/tsfeatures.ipynb 28
601
+ # %% ../nbs/tsfeatures.ipynb 27
599
602
def series_length (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
600
603
"""Series length.
601
604
@@ -614,7 +617,7 @@ def series_length(x: np.array, freq: int = 1) -> Dict[str, float]:
614
617
615
618
return {"series_length" : len (x )}
616
619
617
- # %% ../nbs/tsfeatures.ipynb 29
620
+ # %% ../nbs/tsfeatures.ipynb 28
618
621
def sparsity (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
619
622
"""Sparsity.
620
623
@@ -633,7 +636,7 @@ def sparsity(x: np.array, freq: int = 1) -> Dict[str, float]:
633
636
634
637
return {"sparsity" : np .mean (x == 0 )}
635
638
636
- # %% ../nbs/tsfeatures.ipynb 30
639
+ # %% ../nbs/tsfeatures.ipynb 29
637
640
def stability (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
638
641
"""Stability.
639
642
@@ -667,7 +670,7 @@ def stability(x: np.array, freq: int = 1) -> Dict[str, float]:
667
670
668
671
return {"stability" : stability }
669
672
670
- # %% ../nbs/tsfeatures.ipynb 31
673
+ # %% ../nbs/tsfeatures.ipynb 30
671
674
def stl_features (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
672
675
"""Calculates seasonal trend using loess decomposition.
673
676
@@ -785,8 +788,14 @@ def stl_features(x: np.array, freq: int = 1) -> Dict[str, float]:
785
788
time_x = add_constant (poly_m )
786
789
coefs = OLS (trend0 , time_x ).fit ().params
787
790
788
- linearity = coefs [1 ]
789
- curvature = - coefs [2 ]
791
+ try :
792
+ linearity = coefs [1 ]
793
+ except :
794
+ linearity = np .nan
795
+ try :
796
+ curvature = - coefs [2 ]
797
+ except :
798
+ curvature = np .nan
790
799
# ACF features
791
800
acfremainder = acf_features (remainder , m )
792
801
# Assemble features
@@ -808,7 +817,7 @@ def stl_features(x: np.array, freq: int = 1) -> Dict[str, float]:
808
817
809
818
return output
810
819
811
- # %% ../nbs/tsfeatures.ipynb 32
820
+ # %% ../nbs/tsfeatures.ipynb 31
812
821
def unitroot_kpss (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
813
822
"""Unit root kpss.
814
823
@@ -834,7 +843,7 @@ def unitroot_kpss(x: np.array, freq: int = 1) -> Dict[str, float]:
834
843
835
844
return {"unitroot_kpss" : test_kpss }
836
845
837
- # %% ../nbs/tsfeatures.ipynb 33
846
+ # %% ../nbs/tsfeatures.ipynb 32
838
847
def unitroot_pp (x : np .array , freq : int = 1 ) -> Dict [str , float ]:
839
848
"""Unit root pp.
840
849
@@ -884,7 +893,6 @@ def _get_feats(
884
893
],
885
894
dict_freqs = FREQS ,
886
895
):
887
- print ("dict_freq" )
888
896
if freq is None :
889
897
inf_freq = pd .infer_freq (ts ["ds" ])
890
898
if inf_freq is None :
0 commit comments