Spaces:
Sleeping
Sleeping
Daniel Varga
commited on
Commit
•
e79546e
1
Parent(s):
f1abbf3
testing sktime
Browse files- v2/test_predictor_sktime.py +66 -0
v2/test_predictor_sktime.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
|
4 |
+
from sktime.datasets import load_airline
|
5 |
+
from sktime.forecasting.base import ForecastingHorizon
|
6 |
+
from sktime.forecasting.naive import NaiveForecaster
|
7 |
+
from sktime.forecasting.arima import AutoARIMA
|
8 |
+
from sktime.forecasting.ets import AutoETS
|
9 |
+
|
10 |
+
from sktime.performance_metrics.forecasting import MeanAbsolutePercentageError
|
11 |
+
from sktime.split import temporal_train_test_split
|
12 |
+
from sktime.split import ExpandingWindowSplitter
|
13 |
+
from sktime.forecasting.model_evaluation import evaluate
|
14 |
+
from sktime.utils.plotting import plot_series
|
15 |
+
|
16 |
+
|
17 |
+
# step 1: splitting historical data
|
18 |
+
y = load_airline()
|
19 |
+
|
20 |
+
# forecaster = NaiveForecaster(strategy="last", sp=12)
|
21 |
+
forecaster = AutoETS(auto=True, sp=12, n_jobs=-1)
|
22 |
+
# forecaster = AutoARIMA(sp=12, suppress_warnings=True)
|
23 |
+
|
24 |
+
cv = ExpandingWindowSplitter(
|
25 |
+
step_length=12, fh=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], initial_window=72
|
26 |
+
)
|
27 |
+
|
28 |
+
df = evaluate(forecaster=forecaster, y=y, cv=cv, strategy="refit", return_data=True)
|
29 |
+
|
30 |
+
print(df['y_pred'])
|
31 |
+
|
32 |
+
fig, ax = plot_series(
|
33 |
+
y,
|
34 |
+
df["y_pred"].iloc[0],
|
35 |
+
df["y_pred"].iloc[1],
|
36 |
+
df["y_pred"].iloc[2],
|
37 |
+
df["y_pred"].iloc[3],
|
38 |
+
df["y_pred"].iloc[4],
|
39 |
+
df["y_pred"].iloc[5],
|
40 |
+
markers=["o", "", "", "", "", "", ""],
|
41 |
+
labels=["y_true"] + ["y_pred (Backtest " + str(x) + ")" for x in range(6)],
|
42 |
+
)
|
43 |
+
ax.legend()
|
44 |
+
plt.show()
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
y_train, y_test = temporal_train_test_split(y, test_size=len(y.index) // 2)
|
49 |
+
|
50 |
+
# step 2: running the basic forecasting workflow
|
51 |
+
fh = ForecastingHorizon(y_test.index, is_relative=False)
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
forecaster.fit(y_train)
|
56 |
+
y_pred = forecaster.predict(fh)
|
57 |
+
|
58 |
+
plot_series(y_train, y_test, y_pred, labels=["y_train", "y_test", "y_pred"])
|
59 |
+
plt.show()
|
60 |
+
|
61 |
+
# step 3: specifying the evaluation metric
|
62 |
+
mape = MeanAbsolutePercentageError(symmetric=False)
|
63 |
+
# if function interface is used, just use the function directly in step 4
|
64 |
+
|
65 |
+
# step 4: computing the forecast performance
|
66 |
+
print(mape(y_test, y_pred))
|