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.preprocessing import StandardScaler
from sktime.forecasting.compose import TransformedTargetForecaster
from sktime.forecasting.model_selection import ForecastingGridSearchCV, SlidingWindowSplitter
from sktime.forecasting.naive import NaiveForecaster
from sktime.transformations.compose import OptionalPassthrough
from sktime.transformations.series.adapt import TabularToSeriesAdaptor
from sktime.transformations.series.detrend import Deseasonalizer
# decide if use a transformer with OptionalPassthrough
# passthrough, False, use the transformer within, True, ignores the transformer within
pipe = TransformedTargetForecaster(
steps=[
("deseasonalizer", OptionalPassthrough(Deseasonalizer())),
("scaler", OptionalPassthrough(TabularToSeriesAdaptor(StandardScaler()))),
("forecaster", NaiveForecaster()),
]
)
pipe.get_params()
{'steps': [('deseasonalizer', OptionalPassthrough(transformer=Deseasonalizer())), ('scaler', OptionalPassthrough(transformer=TabularToSeriesAdaptor(transformer=StandardScaler()))), ('forecaster', NaiveForecaster())], 'deseasonalizer': OptionalPassthrough(transformer=Deseasonalizer()), 'scaler': OptionalPassthrough(transformer=TabularToSeriesAdaptor(transformer=StandardScaler())), 'forecaster': NaiveForecaster(), 'deseasonalizer__passthrough': False, 'deseasonalizer__transformer__model': 'additive', 'deseasonalizer__transformer__sp': 1, 'deseasonalizer__transformer': Deseasonalizer(), 'scaler__passthrough': False, 'scaler__transformer__fit_in_transform': False, 'scaler__transformer__transformer__copy': True, 'scaler__transformer__transformer__with_mean': True, 'scaler__transformer__transformer__with_std': True, 'scaler__transformer__transformer': StandardScaler(), 'scaler__transformer': TabularToSeriesAdaptor(transformer=StandardScaler()), 'forecaster__sp': 1, 'forecaster__strategy': 'last', 'forecaster__window_length': None}
cv = SlidingWindowSplitter(initial_window=60, window_length=24, start_with_window=True, step_length=24)
param_grid = {
"deseasonalizer__passthrough": [True, False],
"scaler__transformer__transformer__with_mean": [True, False],
"scaler__passthrough": [True, False],
"forecaster__strategy": ["drift", "mean", "last"],
}
gscv = ForecastingGridSearchCV(forecaster=pipe, param_grid=param_grid, cv=cv, n_jobs=-1)
selection = gscv.fit(y_train)
selection.best_params_
{'deseasonalizer__passthrough': True, 'forecaster__strategy': 'drift', 'scaler__passthrough': True, 'scaler__transformer__transformer__with_mean': True}
model = selection.best_forecaster_
model
TransformedTargetForecaster(steps=[('deseasonalizer', OptionalPassthrough(passthrough=True, transformer=Deseasonalizer())), ('scaler', OptionalPassthrough(passthrough=True, transformer=TabularToSeriesAdaptor(transformer=StandardScaler()))), ('forecaster', NaiveForecaster(strategy='drift'))])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
TransformedTargetForecaster(steps=[('deseasonalizer', OptionalPassthrough(passthrough=True, transformer=Deseasonalizer())), ('scaler', OptionalPassthrough(passthrough=True, transformer=TabularToSeriesAdaptor(transformer=StandardScaler()))), ('forecaster', NaiveForecaster(strategy='drift'))])