from sktime.datasets import load_airline
from sktime.forecasting.model_selection import temporal_train_test_split
y = load_airline() # 144 for 12 years
y_train, y_test = temporal_train_test_split(y, test_size=36) # hold out last 3 years
from sklearn.neighbors import KNeighborsRegressor
from sktime.forecasting.compose import make_reduction
from sktime.forecasting.model_selection import ForecastingGridSearchCV, SlidingWindowSplitter
regressor = KNeighborsRegressor() # a model from sklearn
forecaster = make_reduction(regressor, window_length=15, strategy="recursive")
forecaster.get_params()
{'estimator__algorithm': 'auto', 'estimator__leaf_size': 30, 'estimator__metric': 'minkowski', 'estimator__metric_params': None, 'estimator__n_jobs': None, 'estimator__n_neighbors': 5, 'estimator__p': 2, 'estimator__weights': 'uniform', 'estimator': KNeighborsRegressor(), 'pooling': 'local', 'transformers': None, 'window_length': 15}
param_grid = {"window_length": [7, 12, 15], 'estimator__n_neighbors':[1, 3, 5, 7]}
cv = SlidingWindowSplitter(initial_window=int(len(y_train) * 0.8), window_length=72)
gscv = ForecastingGridSearchCV(
forecaster, strategy="refit", cv=cv, param_grid=param_grid
)
fine_tuning = gscv.fit(y_train)
fine_tuning.best_params_
{'estimator__n_neighbors': 3, 'window_length': 12}
import numpy as np
model = fine_tuning.best_forecaster_
fh = np.arange(1, 37)
y_pred = fine_tuning.predict(fh)
from sktime.utils.plotting import plot_series
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
plot_series(y_train, y_test, y_pred, labels=["y_train", "y_test", "y_pred"])
mean_absolute_percentage_error(y_test, y_pred, symmetric=False)
0.12091611131563382