In [1]:
import pandas as pd
import numpy as np
import pyaf.ForecastEngine as autof
import pyaf.Bench.TS_datasets as tsds
%matplotlib inline
In [2]:
import datetime as dt
from dateutil.relativedelta import relativedelta
def addMonths(iTime , iMonths):
lTime = iTime
date_after_month = lTime + relativedelta(months=iMonths)
#print(lTime, iMonths, date_after_month);
return np.datetime64(date_after_month)
In [3]:
b1 = tsds.load_ozone()
df = b1.mPastData
Month Ozone Time
0 1955-01 2.7 1955-01-01
1 1955-02 2.0 1955-02-01
2 1955-03 3.6 1955-03-01
3 1955-04 5.0 1955-04-01
4 1955-05 6.5 1955-05-01
In [4]:
df.head()
Out[4]:
Month
Ozone
Time
0
1955-01
2.7
1955-01-01
1
1955-02
2.0
1955-02-01
2
1955-03
3.6
1955-03-01
3
1955-04
5.0
1955-04-01
4
1955-05
6.5
1955-05-01
In [5]:
df.Time.describe()
Out[5]:
count 204
unique 204
top 1970-02-01 00:00:00
freq 1
first 1955-01-01 00:00:00
last 1971-12-01 00:00:00
Name: Time, dtype: object
In [6]:
x = pd.Series(range(3,10));
df.head()
Out[6]:
Month
Ozone
Time
0
1955-01
2.7
1955-01-01
1
1955-02
2.0
1955-02-01
2
1955-03
3.6
1955-03-01
3
1955-04
5.0
1955-04-01
4
1955-05
6.5
1955-05-01
In [7]:
def replicate(df , K):
N = df.shape[0];
y = pd.Series(range(K*N));
tmin = df.Time.min()
Time1 = y.apply(lambda x : addMonths(tmin , x))
# Time1.describe()
Ozone1 = pd.concat([df.Ozone] * K)
print(Time1.shape, Ozone1.shape, Time1.describe(), Ozone1.describe())
df1 = pd.DataFrame();
df1['Time'] = Time1.values;
print(Ozone1.describe());
df1['Ozone'] = Ozone1.values;
return df1;
In [8]:
df1 = replicate(df, 12);
df1.head()
(2448,) (2448,) count 2448
unique 2448
top 2128-09-01 00:00:00
freq 1
first 1955-01-01 00:00:00
last 2158-12-01 00:00:00
dtype: object count 2448.000000
mean 3.835784
std 1.491864
min 1.200000
25% 2.600000
50% 3.750000
75% 4.825000
max 8.700000
Name: Ozone, dtype: float64
count 2448.000000
mean 3.835784
std 1.491864
min 1.200000
25% 2.600000
50% 3.750000
75% 4.825000
max 8.700000
Name: Ozone, dtype: float64
Out[8]:
Time
Ozone
0
1955-01-01
2.7
1
1955-02-01
2.0
2
1955-03-01
3.6
3
1955-04-01
5.0
4
1955-05-01
6.5
In [9]:
df1.describe()
Out[9]:
Ozone
count
2448.000000
mean
3.835784
std
1.491864
min
1.200000
25%
2.600000
50%
3.750000
75%
4.825000
max
8.700000
In [10]:
lEngine = autof.cForecastEngine()
lEngine.mOptions.mEnableSeasonals = True;
lEngine
#lEngine.mOptions.enable_slow_mode()
#lEngine.mOptions.mCycle_Criterion = "L2";
#lEngine.mOptions.mCycle_Criterion_Threshold = 10000.2;
Out[10]:
<pyaf.ForecastEngine.cForecastEngine at 0x7f4cf8a3cef0>
In [11]:
lEngine.train(df1 , 'Time' , 'Ozone', 12)
In [12]:
lEngine.getModelInfo()
In [ ]:
In [13]:
type1 = np.dtype(df.Time)
In [14]:
type1.kind
Out[14]:
'M'
In [15]:
lEngine.mSignalDecomposition.mTrPerfDetails
Out[15]:
Transformation
Model
Complexity
FitCount
FitL2
FitMAPE
ForecastCount
ForecastL2
ForecastMAPE
TestCount
TestL2
TestMAPE
0
_Ozone
_Ozone_ConstantTrend_residue_Seasonal_MonthOfY...
260
1948
0.221387
0.0280
488
0.059431
0.0152
12
0.045705
0.0192
1
_Ozone
_Ozone_ConstantTrend_residue_bestCycle_byL2_re...
264
1948
0.221387
0.0280
488
0.059431
0.0152
12
0.045705
0.0192
2
_Ozone
_Ozone_LinearTrend_residue_Seasonal_MonthOfYea...
276
1948
0.221401
0.0280
488
0.060049
0.0154
12
0.049996
0.0204
3
_Ozone
_Ozone_PolyTrend_residue_Seasonal_MonthOfYear_...
276
1948
0.221326
0.0281
488
0.060951
0.0154
12
0.042905
0.0186
4
_Ozone
_Ozone_LinearTrend_residue_bestCycle_byL2_resi...
280
1948
0.221401
0.0280
488
0.060049
0.0154
12
0.049996
0.0204
5
_Ozone
_Ozone_PolyTrend_residue_bestCycle_byL2_residu...
280
1948
0.221326
0.0281
488
0.060951
0.0154
12
0.042905
0.0186
6
_Ozone
_Ozone_ConstantTrend_residue_zeroCycle_residue...
256
1948
0.229781
0.0285
488
0.066387
0.0158
12
0.045819
0.0175
7
_Ozone
_Ozone_Lag1Trend_residue_zeroCycle_residue_AR(...
288
1948
0.245049
0.0301
488
0.067435
0.0166
12
0.045353
0.0180
8
_Ozone
_Ozone_Lag1Trend_residue_Seasonal_MonthOfYear_...
292
1948
0.229343
0.0301
488
0.066485
0.0166
12
0.074977
0.0260
9
_Ozone
_Ozone_Lag1Trend_residue_bestCycle_byL2_residu...
296
1948
0.229343
0.0301
488
0.066485
0.0166
12
0.074977
0.0260
10
CumSum_Ozone
CumSum_Ozone_Lag1Trend_residue_Seasonal_MonthO...
324
1948
0.312026
0.0417
488
0.088757
0.0231
12
0.062862
0.0242
11
CumSum_Ozone
CumSum_Ozone_Lag1Trend_residue_bestCycle_byL2_...
328
1948
0.312026
0.0417
488
0.088757
0.0231
12
0.062862
0.0242
12
_Ozone
_Ozone_LinearTrend_residue_zeroCycle_residue_A...
272
1948
0.231021
0.0298
488
0.089181
0.0234
12
0.090510
0.0363
13
CumSum_Ozone
CumSum_Ozone_LinearTrend_residue_Seasonal_Mont...
308
1948
0.321251
0.0428
488
0.098397
0.0235
12
0.043573
0.0172
14
CumSum_Ozone
CumSum_Ozone_PolyTrend_residue_Seasonal_MonthO...
308
1948
0.337452
0.0454
488
0.102543
0.0239
12
0.037782
0.0145
15
CumSum_Ozone
CumSum_Ozone_PolyTrend_residue_bestCycle_byL2_...
312
1948
0.337452
0.0454
488
0.102543
0.0239
12
0.037782
0.0145
16
CumSum_Ozone
CumSum_Ozone_ConstantTrend_residue_zeroCycle_r...
288
1948
0.353976
0.0450
488
0.102541
0.0241
12
0.069419
0.0223
17
CumSum_Ozone
CumSum_Ozone_LinearTrend_residue_zeroCycle_res...
304
1948
0.343466
0.0431
488
0.103703
0.0243
12
0.041819
0.0181
18
CumSum_Ozone
CumSum_Ozone_PolyTrend_residue_zeroCycle_resid...
304
1948
0.341692
0.0431
488
0.103293
0.0243
12
0.042016
0.0185
19
CumSum_Ozone
CumSum_Ozone_Lag1Trend_residue_zeroCycle_resid...
320
1948
0.357806
0.0456
488
0.103133
0.0244
12
0.069771
0.0232
20
_Ozone
_Ozone_PolyTrend_residue_zeroCycle_residue_AR(...
272
1948
0.230646
0.0298
488
0.106619
0.0268
12
0.130112
0.0529
21
Diff_Ozone
Diff_Ozone_Lag1Trend_residue_zeroCycle_residue...
320
1948
0.463895
0.0809
488
0.299489
0.0627
12
0.316521
0.1456
22
CumSum_Ozone
CumSum_Ozone_LinearTrend_residue_bestCycle_byL...
312
1948
0.449494
0.0923
488
0.313337
0.0803
12
0.313013
0.1102
23
CumSum_Ozone
CumSum_Ozone_ConstantTrend_residue_Seasonal_Mo...
292
1948
1.054658
0.1240
488
0.370859
0.0896
12
0.396125
0.1454
24
CumSum_Ozone
CumSum_Ozone_ConstantTrend_residue_bestCycle_b...
296
1948
1.054658
0.1240
488
0.370859
0.0896
12
0.396125
0.1454
25
Diff_Ozone
Diff_Ozone_ConstantTrend_residue_Seasonal_Mont...
292
1948
0.833121
0.1773
488
0.575918
0.1488
12
0.569177
0.2430
26
Diff_Ozone
Diff_Ozone_ConstantTrend_residue_bestCycle_byL...
296
1948
0.833121
0.1773
488
0.575918
0.1488
12
0.569177
0.2430
27
Diff_Ozone
Diff_Ozone_ConstantTrend_residue_Seasonal_Mont...
36
1948
1.124367
0.2515
488
1.108591
0.2063
12
1.037508
0.3714
28
Diff_Ozone
Diff_Ozone_ConstantTrend_residue_bestCycle_byL...
40
1948
1.124367
0.2515
488
1.108591
0.2063
12
1.037508
0.3714
29
_Ozone
_Ozone_Lag1Trend_residue_Seasonal_MonthOfYear_...
36
1948
0.954001
0.2077
488
0.935426
0.2096
12
0.477270
0.1910
...
...
...
...
...
...
...
...
...
...
...
...
...
66
Diff_Ozone
Diff_Ozone_PolyTrend_residue_bestCycle_byL2_re...
56
1948
1.543234
0.4443
488
2.178290
0.4966
12
2.492624
1.0986
67
Diff_Ozone
Diff_Ozone_PolyTrend_residue_zeroCycle_residue...
48
1948
1.557037
0.4054
488
2.579439
0.5299
12
2.653698
1.0371
68
Diff_Ozone
Diff_Ozone_PolyTrend_residue_Seasonal_MonthOfY...
308
1948
1.430274
0.3872
488
2.585072
0.6736
12
5.802103
2.5979
69
Diff_Ozone
Diff_Ozone_PolyTrend_residue_bestCycle_byL2_re...
312
1948
1.430274
0.3872
488
2.585072
0.6736
12
5.802103
2.5979
70
CumSum_Ozone
CumSum_Ozone_ConstantTrend_residue_zeroCycle_r...
32
1948
85.666131
1.7175
488
4.050574
1.0000
12
2.578275
1.0000
71
CumSum_Ozone
CumSum_Ozone_ConstantTrend_residue_Seasonal_Mo...
36
1948
85.956671
2.3476
488
9.472656
1.6507
12
9.162234
2.3340
72
CumSum_Ozone
CumSum_Ozone_ConstantTrend_residue_bestCycle_b...
40
1948
85.956671
2.3476
488
9.472656
1.6507
12
9.162234
2.3340
73
RelDiff_Ozone
RelDiff_Ozone_ConstantTrend_residue_zeroCycle_...
32
1948
5.011929
1.6394
488
5.136152
1.7245
12
6.266777
2.8884
74
RelDiff_Ozone
RelDiff_Ozone_ConstantTrend_residue_Seasonal_M...
36
1948
5.002485
1.6354
488
5.136152
1.7245
12
6.266777
2.8884
75
RelDiff_Ozone
RelDiff_Ozone_ConstantTrend_residue_bestCycle_...
40
1948
5.002485
1.6354
488
5.136152
1.7245
12
6.266777
2.8884
76
RelDiff_Ozone
RelDiff_Ozone_LinearTrend_residue_zeroCycle_re...
48
1948
5.016847
1.6417
488
5.136152
1.7245
12
6.266777
2.8884
77
RelDiff_Ozone
RelDiff_Ozone_PolyTrend_residue_zeroCycle_resi...
48
1948
5.018841
1.6426
488
5.136152
1.7245
12
6.266777
2.8884
78
RelDiff_Ozone
RelDiff_Ozone_LinearTrend_residue_Seasonal_Mon...
52
1948
5.009341
1.6385
488
5.136152
1.7245
12
6.266777
2.8884
79
RelDiff_Ozone
RelDiff_Ozone_PolyTrend_residue_Seasonal_Month...
52
1948
5.011928
1.6397
488
5.136152
1.7245
12
6.266777
2.8884
80
RelDiff_Ozone
RelDiff_Ozone_LinearTrend_residue_bestCycle_by...
56
1948
5.009341
1.6385
488
5.136152
1.7245
12
6.266777
2.8884
81
RelDiff_Ozone
RelDiff_Ozone_PolyTrend_residue_bestCycle_byL2...
56
1948
5.011928
1.6397
488
5.136152
1.7245
12
6.266777
2.8884
82
RelDiff_Ozone
RelDiff_Ozone_ConstantTrend_residue_zeroCycle_...
288
1948
4.980849
1.6226
488
5.136152
1.7245
12
6.266777
2.8884
83
RelDiff_Ozone
RelDiff_Ozone_ConstantTrend_residue_Seasonal_M...
292
1948
4.968838
1.6181
488
5.136152
1.7245
12
6.266777
2.8884
84
RelDiff_Ozone
RelDiff_Ozone_ConstantTrend_residue_bestCycle_...
296
1948
4.968838
1.6181
488
5.136152
1.7245
12
6.266777
2.8884
85
RelDiff_Ozone
RelDiff_Ozone_LinearTrend_residue_zeroCycle_re...
304
1948
4.991305
1.6276
488
5.136152
1.7245
12
6.266777
2.8884
86
RelDiff_Ozone
RelDiff_Ozone_PolyTrend_residue_zeroCycle_resi...
304
1948
4.996328
1.6300
488
5.136152
1.7245
12
6.266777
2.8884
87
RelDiff_Ozone
RelDiff_Ozone_LinearTrend_residue_Seasonal_Mon...
308
1948
4.970459
1.6189
488
5.136152
1.7245
12
6.266777
2.8884
88
RelDiff_Ozone
RelDiff_Ozone_PolyTrend_residue_Seasonal_Month...
308
1948
4.970952
1.6191
488
5.136152
1.7245
12
6.266777
2.8884
89
RelDiff_Ozone
RelDiff_Ozone_LinearTrend_residue_bestCycle_by...
312
1948
4.970459
1.6189
488
5.136152
1.7245
12
6.266777
2.8884
90
RelDiff_Ozone
RelDiff_Ozone_PolyTrend_residue_bestCycle_byL2...
312
1948
4.970952
1.6191
488
5.136152
1.7245
12
6.266777
2.8884
91
RelDiff_Ozone
RelDiff_Ozone_Lag1Trend_residue_zeroCycle_resi...
320
1948
4.998208
1.6358
488
5.136152
1.7245
12
6.266777
2.8884
92
RelDiff_Ozone
RelDiff_Ozone_Lag1Trend_residue_Seasonal_Month...
324
1948
4.887302
1.5817
488
5.136152
1.7245
12
6.266777
2.8884
93
RelDiff_Ozone
RelDiff_Ozone_Lag1Trend_residue_bestCycle_byL2...
328
1948
4.887302
1.5817
488
5.136152
1.7245
12
6.266777
2.8884
94
Diff_Ozone
Diff_Ozone_LinearTrend_residue_zeroCycle_resid...
304
1948
5.655509
1.5493
488
7.166906
1.9159
12
13.334821
5.9647
95
Diff_Ozone
Diff_Ozone_PolyTrend_residue_zeroCycle_residue...
304
1948
6.130249
1.7245
488
14.039135
3.6441
12
28.649102
12.8181
96 rows × 12 columns
In [16]:
lEngine.standrdPlots()
/usr/lib/python3/dist-packages/matplotlib/__init__.py:1403: UserWarning: This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
warnings.warn(_use_error_msg)
In [17]:
lEngine.mSignalDecomposition.mBestModel.mTimeInfo.mTimeDelta
Out[17]:
Timedelta('30 days 10:28:40')
In [ ]:
Content source: antoinecarme/pyaf
Similar notebooks: