Skip to content

ForecasterAutoreg

class
skforecast.ForecasterAutoreg.ForecasterAutoreg(regressor, lags)

This class turns any regressor compatible with the scikit-learn API into a recursive autoregressive (multi-step) forecaster.

Parameters
  • regressor (regressor compatible with the scikit-learn API) An instance of a regressor compatible with the scikit-learn API.
  • lags (int, list, 1D np.array, range) Lags used as predictors. Index starts at 1, so lag 1 is equal to t-1. int: include lags from 1 to lags (included). list or np.array: include only lags present in lags.
Attributes
  • exog_shape (tuple) Shape of exog used in training.
  • exog_type (type) Type used for the exogenous variable/s: pd.Series, pd.DataFrame or np.ndarray.
  • 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.
  • lags (1D np.array) Lags used as predictors.
  • last_window (1D np.ndarray) Last time window the forecaster has seen when trained. It stores the values needed to calculate the lags used to predict the next step after the training data.
  • max_lag (int) Maximum value of lag included in lags.
  • 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.
Methods
  • __repr__() (str) Information displayed when a ForecasterAutoreg object is printed.
  • create_lags(y) (X_data : 2D np.ndarray, shape (samples, len(self.lags))) Transforms a time series into a 2D array and a 1D array where each value of y is associated with the lags that precede it.
  • 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 : ForecasterAutoreg) Training ForecasterAutoreg
  • 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) (predictions : 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_lags(lags) (self) Set new value to the attribute lags. Attribute max_lag is also updated.
  • set_out_sample_residuals(residuals) (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 ForecasterAutoreg.
method
__repr__() → str

Information displayed when a ForecasterAutoreg object is printed.

method
create_lags(y)

Transforms a time series into a 2D array and a 1D array where each value of y is associated with the lags that precede it.

Notice that the returned matrix X_data, contains the lag 1 in the first column, the lag 2 in the second column and so on.

Parameters
  • y (1D np.ndarray, pd.Series) Training time series.
Returns (X_data : 2D np.ndarray, shape (samples, len(self.lags)))

2D array with the lag values (predictors).

ta : 1D np.ndarray, shape (nÂș observaciones - max(seld.lags),) Values of the time series related to each row of X_data.

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 ForecasterAutoreg

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 : ForecasterAutoreg)

Trained ForecasterAutoreg

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

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

Returns (predictions : 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.

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 ForecasterAutoreg.

Parameters
  • params (dict) Parameters values.
method
set_lags(lags)

Set new value to the attribute lags. Attribute max_lag is also updated.

Parameters
  • Lags used as predictors. Index starts at 1, so lag 1 is equal to t-1. int: include lags from 1 to lags. list or np.array: include only lags present in lags.
method
set_out_sample_residuals(residuals)

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
  • 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 (lag). Coefficients are aligned so that coef[i] is the value associated with self.lags[i].

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 (lag). Values are aligned so that feature_importances[i] is the value associated with self.lags[i].