ForecasterAutoreg
¶
skforecast.ForecasterAutoreg.
ForecasterAutoreg
(
regressor
, lags
)
This class turns any regressor compatible with the scikit-learn API into a recursive autoregressive (multi-step) forecaster.
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 tolags
(included).list
ornp.array
: include only lags present inlags
.
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 nextstep
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.
__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 ofy
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, yfit
(
y
,exog
)
(self : ForecasterAutoreg) — Training ForecasterAutoregget_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 asregressor:
LinearRegression(),
Lasso()or
Ridge()`.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 usingregressor=GradientBoostingRegressor()
orregressor=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 attributelags
. Attributemax_lag
is also updated.set_out_sample_residuals
(
residuals
)
(self) — Set new values to the attributeout_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.
__repr__
(
)
→ strInformation displayed when a ForecasterAutoreg object is printed.
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.
y
(1D np.ndarray, pd.Series) — Training time series.
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
.
create_train_X_y
(
y
, exog=None
)
Create training matrices X, y
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 asy
and should be aligned so that y[i] is regressed on exog[i].
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
.
fit
(
y
, exog=None
)
Training ForecasterAutoreg
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 asy
and should be aligned so that y[i] is regressed on exog[i].
Trained ForecasterAutoreg
predict
(
steps
, last_window=None
, exog=None
)
Iterative process in which, each prediction, is used as a predictor for the next step.
Values predicted.
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.
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.
set_params
(
**params
)
Set new values to the parameters of the scikit learn model stored in the ForecasterAutoreg.
params
(dict) — Parameters values.
set_lags
(
lags
)
Set new value to the attribute lags
.
Attribute max_lag
is also updated.
Lags used as predictors. Index starts at 1, so lag 1 is equal to t-1.
—int
: include lags from 1 tolags
.list
ornp.array
: include only lags present inlags
.
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.
params
(1D np.ndarray) — Values of residuals. If len(residuals) > 1000, only a random sample of 1000 values are stored.
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()or
Ridge()`.
Value of the coefficients associated with each predictor (lag).
Coefficients are aligned so that coef[i]
is the value associated
with self.lags[i]
.
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
.
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]
.