gtime.forecasting
.GAR¶
-
class
gtime.forecasting.
GAR
(estimator, n_jobs: int = None)¶ Generalized Auto Regression model.
This model is a wrapper of
sklearn.multioutput.MultiOutputRegressor
but returns apd.DataFrame
.Fit one model for each target variable contained in the
y
matrix.- Parameters
- estimatorestimator object, required
The model used to make the predictions step by step. Regressor object such as derived from
RegressorMixin
.- n_jobsint, optional, default:
None
The number of jobs to use for the parallelization.
Examples
>>> import numpy as np >>> import pandas as pd >>> from gtime.forecasting import GAR >>> from sklearn.ensemble import RandomForestRegressor >>> time_index = pd.date_range("2020-01-01", "2020-01-30") >>> X = pd.DataFrame(np.random.random((30, 5)), index=time_index) >>> y_columns = ["y_1", "y_2", "y_3"] >>> y = pd.DataFrame(np.random.random((30, 3)), index=time_index, columns=y_columns) >>> X_train, y_train = X[:20], y[:20] >>> X_test, y_test = X[20:], y[20:] >>> random_forest = RandomForestRegressor() >>> gar = GAR(estimator=random_forest) >>> gar.fit(X_train, y_train) >>> predictions = gar.predict(X_test) >>> predictions.shape (10, 3)
Methods
fit
(self, X, y[, sample_weight])Fit the model.
get_params
(self[, deep])Get parameters for this estimator.
partial_fit
(self, X, y[, sample_weight])Incrementally fit the model to data.
predict
(self, X)For each row in
X
, make a prediction for each fitted model, from 1 tohorizon
.score
(self, X, y[, sample_weight])Returns the coefficient of determination R^2 of the prediction.
set_params
(self, \*\*params)Set the parameters of this estimator.
-
__init__
(self, estimator, n_jobs:int=None)¶ Initialize self. See help(type(self)) for accurate signature.
-
fit
(self, X:pandas.core.frame.DataFrame, y:pandas.core.frame.DataFrame, sample_weight=None)¶ Fit the model.
Train the models, one for each target variable in y.
- Parameters
- Xpd.DataFrame, shape (n_samples, n_features), required.
The data.
- ypd.DataFrame, shape (n_samples, horizon), required.
The matrix containing the target variables.
- Returns
- selfobject
-
get_params
(self, deep=True)¶ Get parameters for this estimator.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsmapping of string to any
Parameter names mapped to their values.
-
partial_fit
(self, X, y, sample_weight=None)¶ Incrementally fit the model to data. Fit a separate model for each output variable.
- Parameters
- X(sparse) array-like, shape (n_samples, n_features)
Data.
- y(sparse) array-like, shape (n_samples, n_outputs)
Multi-output targets.
- sample_weightarray-like of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted. Only supported if the underlying regressor supports sample weights.
- Returns
- selfobject
-
predict
(self, X:pandas.core.frame.DataFrame) → pandas.core.frame.DataFrame¶ For each row in
X
, make a prediction for each fitted model, from 1 tohorizon
.- Parameters
- Xpd.DataFrame, shape (n_samples, n_features), required
The data.
- Returns
- y_p_dfpd.DataFrame, shape (n_samples, horizon)
The predictions, one for each timestep in horizon.
-
score
(self, X, y, sample_weight=None)¶ Returns the coefficient of determination R^2 of the prediction.
The coefficient R^2 is defined as (1 - u/v), where u is the residual sum of squares ((y_true - y_pred) ** 2).sum() and v is the regression sum of squares ((y_true - y_true.mean()) ** 2).sum(). Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0.
- Parameters
- Xarray-like, shape (n_samples, n_features)
Test samples.
- yarray-like, shape (n_samples) or (n_samples, n_outputs)
True values for X.
- sample_weightarray-like, shape [n_samples], optional
Sample weights.
- Returns
- scorefloat
R^2 of self.predict(X) wrt. y.
Notes
R^2 is calculated by weighting all the targets equally using multioutput=’uniform_average’.
-
set_params
(self, **params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfobject
Estimator instance.