Skip to content

ForecasterAutoregCustom

class
skforecast.ForecasterAutoregCustom.ForecasterAutoregCustom(regressor, fun_predictors, window_size)

This class turns any regressor compatible with the scikit-learn API into a recursive (multi-step) forecaster with a custom function to create predictors.

Parameters
  • regressor (any regressor compatible with the scikit-learn API) An instance of a regressor compatible with the scikit-learn API.
  • fun_predictors (Callable) Function that takes a time series window as an argument and returns an np.array with the predictors associated with that window.
  • window_size (int) Size of the window needed by fun_predictors to create the predictors.
Attributes
  • exog_shape (tuple) Shape of exog used in training.
  • exog_type (type) Type used for the exogenous variable/s.
  • fitted (Bool) Tag to identify if the estimator is fitted.
  • fun_predictors (Callable) Function that takes a time series window as an argument and returns an np.array with the predictors associated with that window.
  • in_sample_residuals (np.ndarray) Residuals of the model when predicting training data. Only stored up to 1000 values.
  • included_exog (bool) If the forecaster has been trained using exogenous variable/s.
  • last_window (1D np.ndarray) Last time window the forecaster has seen when trained. It stores the values needed to calculate the predictors for the next step after the training data.
  • out_sample_residuals (np.ndarray) Residuals of the model when predicting non training data. Only stored up to 1000 values.
  • regressor (regressor compatible with the scikit-learn API) An instance of a regressor compatible with the scikit-learn API.
  • window_size (int) Size of the window needed by fun_predictors to create the predictors.
Methods
  • __repr__() (str) Information displayed when a ForecasterAutoregCustom object is printed.
  • create_train_X_y(y, exog) (X_train : 2D np.ndarray, shape (len(y) - self.max_lag, len(self.lags))) Create training matrices X, y
  • fit(y, exog) (self : ForecasterAutoregCustom) Training ForecasterAutoregCustom
  • get_coef() (coef : 1D np.ndarray) Return estimated coefficients for the linear regression model stored in the forecaster. Only valid when the forecaster has been trained using as regressor:LinearRegression(),Lasso()orRidge()`.
  • get_feature_importances() (feature_importances : 1D np.ndarray) Return impurity-based feature importances of the model stored in the forecaster. Only valid when the forecaster has been trained using regressor=GradientBoostingRegressor() or regressor=RandomForestRegressor.
  • predict(steps, last_window, exog) (predicciones : 1D np.array, shape (steps,)) Iterative process in which, each prediction, is used as a predictor for the next step.
  • predict_interval(steps, last_window, exog, interval, n_boot, in_sample_residuals) (predictions : np.array, shape (steps, 3)) Iterative process in which, each prediction, is used as a predictor for the next step and bootstrapping is used to estimate prediction intervals. Both, predictions and intervals, are returned.
  • set_out_sample_residuals(residuals, append) (self) Set new values to the attribute out_sample_residuals. Out of sample residuals are meant to be calculated using observations that did not participate in the training process.
  • set_params(**params) (self) Set new values to the parameters of the scikit learn model stored in the ForecasterAutoregCustom.
method
__repr__() → str

Information displayed when a ForecasterAutoregCustom object is printed.

method
create_train_X_y(y, exog=None)

Create training matrices X, y

Parameters
  • y (1D np.ndarray, pd.Series) Training time series.
  • exog (np.ndarray, pd.Series, pd.DataFrame, default `None`) Exogenous variable/s included as predictor/s. Must have the same number of observations as y and should be aligned so that y[i] is regressed on exog[i].
Returns (X_train : 2D np.ndarray, shape (len(y) - self.max_lag, len(self.lags)))

2D array with the training values (predictors).

ain : 1D np.ndarray, shape (len(y) - self.max_lag,) Values (target) of the time series related to each row of X_train.

method
fit(y, exog=None)

Training ForecasterAutoregCustom

Parameters
  • y (1D np.ndarray, pd.Series) Training time series.
  • exog (np.ndarray, pd.Series, pd.DataFrame, default `None`) Exogenous variable/s included as predictor/s. Must have the same number of observations as y and should be aligned so that y[i] is regressed on exog[i].
Returns (self : ForecasterAutoregCustom)

Trained ForecasterAutoregCustom

method
predict(steps, last_window=None, exog=None)

Iterative process in which, each prediction, is used as a predictor for the next step.

Parameters
  • steps (int) Number of future steps predicted.
  • last_window (1D np.ndarray, pd.Series, default `None`) Values of the series used to create the predictors need in the first iteration of predictiont (t + 1).
    If last_window = None, the values stored inself.last_window are used to calculate the initial predictors, and the predictions start right after training data.
  • exog (np.ndarray, pd.Series, pd.DataFrame, default `None`) Exogenous variable/s included as predictor/s.
Returns (predicciones : 1D np.array, shape (steps,))

Values predicted.

method
predict_interval(steps, last_window=None, exog=None, interval=[5, 95], n_boot=500, in_sample_residuals=True)

Iterative process in which, each prediction, is used as a predictor for the next step and bootstrapping is used to estimate prediction intervals. Both, predictions and intervals, are returned.

Parameters
  • steps (int) Number of future steps predicted.
  • last_window (1D np.ndarray, pd.Series, default `None`) Values of the series used to create the predictors need in the first iteration of predictiont (t + 1).
    If last_window = None, the values stored inself.last_window are used to calculate the initial predictors, and the predictions start right after training data.
  • exog (np.ndarray, pd.Series, pd.DataFrame, default `None`) Exogenous variable/s included as predictor/s.
  • interval (list, default `[5, 100]`) Confidence of the prediction interval estimated. Sequence of percentiles to compute, which must be between 0 and 100 inclusive.
  • n_boot (int, default `500`) Number of bootstrapping iterations used to estimate prediction intervals.
  • in_sample_residuals (bool, default `True`) If True, residuals from the training data are used as proxy of prediction error to create prediction intervals. If False, out of sample residuals are used. In the latter case, the user shoud have calculated and stored the residuals within the forecaster (see set_out_sample_residuals()).
Returns (predictions : np.array, shape (steps, 3))

Values predicted by the forecaster and their estimated interval. Column 0 = predictions Column 1 = lower bound interval Column 2 = upper bound interval

Notes

More information about prediction intervals in forecasting: https://otexts.com/fpp2/prediction-intervals.html Forecasting: Principles and Practice (2nd ed) Rob J Hyndman and George Athanasopoulos.

method
set_params(**params)

Set new values to the parameters of the scikit learn model stored in the ForecasterAutoregCustom.

Parameters
  • params (dict) Parameters values.
method
set_out_sample_residuals(residuals, append=True)

Set new values to the attribute out_sample_residuals. Out of sample residuals are meant to be calculated using observations that did not participate in the training process.

Parameters
  • append (bool, default `True`) If True, new residuals are added to the once already stored in the attribute out_sample_residuals. Once the limit of 1000 values is reached, no more values are appended. If False, out_sample_residuals is overwrited with the new residuals.
  • params (1D np.ndarray) Values of residuals. If len(residuals) > 1000, only a random sample of 1000 values are stored.
method
get_coef()

Return estimated coefficients for the linear regression model stored in the forecaster. Only valid when the forecaster has been trained using as regressor:LinearRegression(),Lasso()orRidge()`.

Returns (coef : 1D np.ndarray)

Value of the coefficients associated with each predictor. Coefficients are aligned so that coef[i] is the value associated with predictor i returned by self.create_predictors.

method
get_feature_importances()

Return impurity-based feature importances of the model stored in the forecaster. Only valid when the forecaster has been trained using regressor=GradientBoostingRegressor() or regressor=RandomForestRegressor.

Returns (feature_importances : 1D np.ndarray)

Impurity-based feature importances associated with each predictor. Values are aligned so that feature_importances[i] is the value associated with predictor i returned by self.create_predictors.