[BUG] NotFittedError raised in BaseGridSearch when refit is False
Author: aiwalterCreated Aug 10, 2022Updated Sep 18, 2026
Labelsbugmodule:forecasting
Describe the bug related discussion: #3220
grid search is trying to predict on self.best_forecaster_ = self.forecaster.clone().set_params(**self.best_params_) which was not fitted and raises therefore a NotFittedError. The problem is thatevaluate()does not return the last fitted forecaster but it can optionally return the y_train for each forecaster, so we can take that data to fit the forecaster if not refit :)
To Reproduce
from sktime.datasets import load_shampoo_sales
from sktime.forecasting.model_selection import (
ExpandingWindowSplitter,
ForecastingGridSearchCV,
ExpandingWindowSplitter)
from sktime.forecasting.naive import NaiveForecaster
y = load_shampoo_sales()
fh = [1,2,3]
cv = ExpandingWindowSplitter(
start_with_window=True,
fh=fh)
forecaster = NaiveForecaster()
param_grid = {"strategy" : ["last", "mean", "drift"]}
gscv = ForecastingGridSearchCV(
forecaster=forecaster,
param_grid=param_grid,
cv=cv,
refit=False)
gscv.fit(y)
gscv.predict(fh=[1,2,3])Expected behavior
If refit is False, then the last y_train from cross-validation should be used as fit data
Additional context
Versions
main
Source: sktime/sktime