Fine Tuning
¶

Load Data¶

In [2]:
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

Select Forecaster¶

In [4]:
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")

Get Fine-Turning Parameters¶

In [5]:
forecaster.get_params()
Out[5]:
{'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}

Grid Search¶

In [7]:
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)
In [8]:
fine_tuning.best_params_
Out[8]:
{'estimator__n_neighbors': 3, 'window_length': 12}

Forecasting¶

In [16]:
import numpy as np

model = fine_tuning.best_forecaster_

fh = np.arange(1, 37)
y_pred = fine_tuning.predict(fh)

Evaluation¶

In [17]:
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)
Out[17]:
0.12091611131563382

Reference¶

  • Forecasting