In the Skforecast library, training predictions can be obtained either by using the backtesting_forecaster() function or by accessing the predict() method of the regressor stored inside the forecaster object."
Training predictions with backtesting_forecaster()¶
12345
# Split train-test# ==============================================================================n_backtest=36*3# Last 9 years are used for backtestdata_train=data[:-n_backtest]data_test=data[-n_backtest:]
12345678
# Fit forecaster# ==============================================================================forecaster=ForecasterAutoreg(regressor=RandomForestRegressor(random_state=123),lags=15)forecaster.fit(y=data_train)
Backtesting_forecaster() parameters:
+ Forecaster already trained
+ initial_train_size = None and refit = False
+ If steps = 1, all predictions are t(+1). If steps > 1 the data will be predicted in folds
# Plot training predictions# ==============================================================================n_lags=max(forecaster.lags)print(f"The first {n_lags} observations are not predicted because "\
"it is not possible to create the lags matrix")fig,ax=plt.subplots(figsize=(9,4))data_train.plot(ax=ax,label='train')predictions_train.plot(ax=ax,label='predictions')ax.legend();
The first 15 observations are not predicted because it is not possible to create the lags matrix
# Fit forecaster# ==============================================================================forecaster=ForecasterAutoreg(regressor=RandomForestRegressor(random_state=123),lags=15)forecaster.fit(y=data_train)