gtime.forecasting.TrendForecaster¶
-
class
gtime.forecasting.TrendForecaster(trend: str, trend_x0: numpy.array, loss: Callable = <function mean_squared_error>, method: str = 'BFGS')¶ Trend forecasting model.
This estimator optimizes a trend function on train data and will forecast using this trend function with optimized parameters.
Parameters: - trend :
"polynomial"|"exponential", required The kind of trend removal to apply.
- trend_x0 : np.array, required
Initialisation parameters passed to the trend function
- loss : Callable, optional, default:
mean_squared_error Loss function to minimize.
- method : str, optional, default:
"BFGS" Loss function optimisation method
Examples
>>> import pandas as pd >>> import numpy as np >>> from gtime.model_selection import horizon_shift, FeatureSplitter >>> from gtime.forecasting import TrendForecaster >>> >>> X = pd.DataFrame(np.random.random((10, 1)), index=pd.date_range("2020-01-01", "2020-01-10")) >>> y = horizon_shift(X, horizon=2) >>> X_train, y_train, X_test, y_test = FeatureSplitter().transform(X, y) >>> >>> tf = TrendForecaster(trend='polynomial', trend_x0=np.zeros(2)) >>> tf.fit(X_train).predict(X_test) array([[0.39703029], [0.41734957]])
Methods
fit(self, X[, y])Fit the estimator. get_params(self[, deep])Get parameters for this estimator. predict(self, X)Using the fitted polynomial, predict the values starting from X.score(self, X, y[, sample_weight])Return the coefficient of determination R^2 of the prediction. set_params(self, \*\*params)Set the parameters of this estimator. -
__init__(self, trend: str, trend_x0: <built-in function array>, loss: Callable = <function mean_squared_error at 0x7f5c9a835b70>, method: str = 'BFGS')¶ Initialize self. See help(type(self)) for accurate signature.
-
fit(self, X: pandas.core.frame.DataFrame, y=None) → 'TrendForecaster'¶ Fit the estimator.
Parameters: - X : pd.DataFrame, shape (n_samples, n_features), required
Input data.
- y : None
There is no need of a target in a transformer, yet the pipeline API requires this parameter.
Returns: - self : object
Returns self.
-
get_params(self, deep=True)¶ Get parameters for this estimator.
Parameters: - deep : bool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns: - params : mapping of string to any
Parameter names mapped to their values.
-
predict(self, X: pandas.core.frame.DataFrame) → pandas.core.frame.DataFrame¶ Using the fitted polynomial, predict the values starting from
X.Parameters: - X: pd.DataFrame, shape (n_samples, 1), required
The time series on which to predict.
Returns: - predictions : pd.DataFrame, shape (n_samples, 1)
The output predictions.
Raises: - NotFittedError
Raised if the model is not fitted yet.
-
score(self, X, y, sample_weight=None)¶ Return 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 total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The 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: - X : array-like of shape (n_samples, n_features)
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead, shape = (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
- y : array-like of shape (n_samples,) or (n_samples, n_outputs)
True values for X.
- sample_weight : array-like of shape (n_samples,), default=None
Sample weights.
Returns: - score : float
R^2 of self.predict(X) wrt. y.
Notes
The R2 score used when calling
scoreon a regressor will usemultioutput='uniform_average'from version 0.23 to keep consistent withr2_score. This will influence thescoremethod of all the multioutput regressors (except forMultiOutputRegressor). To specify the default value manually and avoid the warning, please either callr2_scoredirectly or make a custom scorer withmake_scorer(the built-in scorer'r2'usesmultioutput='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: - **params : dict
Estimator parameters.
Returns: - self : object
Estimator instance.
- trend :