StackingEnsemble

class hcrystalball.ensemble.StackingEnsemble(base_learners, meta_model, name='stacking_ensemble', train_n_splits=1, train_horizon=10, horizons_as_features=True, weekdays_as_features=True, fit_meta_model_always=False, clip_predictions_lower=None, clip_predictions_upper=None)[source]

Bases: sklearn.base.BaseEstimator

StackingEnsemble model, which takes a list of any hcrystalball model wrapper instance(s) as base learners.

During fitting the base learners are fitted and prediction(s) will be made for the requested horizon, using possibly more than one splits. The predictions for each model in all splits are concatenated and serve as the feature matrix for the meta model, with the prediction of each model over all splits being a distinct feature. Finally the meta model, which is just a regular regressor, will then be fitted to the data to determine the relative weights of each base learner in the prediction of the ensemble.

As a default behaviour the meta model is fitted only the first time the fit() method is called, then in each subsequent calls of the fit() method (of a given StackingEnsemble instance) omits the fitting of the meta model and fits only the base learners. This behaviour can, however be changed using the fit_meta_model_always parameter to force the meta model to be refitted every time the fit method is called. Note, however, that this latter behaviour can be computationally expensive, as fitting the meta model requires fitting the base learners train_n_splits times.

Parameters
  • name (str) – Unique name / identifier of the model instance

  • base_learners (list) – List of fully instantiated hcrystalball model wrappers

  • meta_model (sklearn.base.BaseEstimator) – Scikit-learn compatible regressor

  • train_n_splits (int) – Number of splits used for fitting the meta model

  • train_horizon (int) – Max. number of steps ahead to be predicted. Ideally this value should not be identical to the forecasting horizon in prediction.

  • horizons_as_features (bool) – Adds horizon feature for meta model

  • weekdays_as_features (bool) – Adds weekdays feature for meta model

  • fit_meta_model_always (bool) – If True the meta model will always be re-fitted, each time the fit() method is called, if False the meta model will only be fitted the first time the fit() method is called and in subsequent calls of the fit() method only the base learners will be re-fitted.

Methods Summary

fit(X[, y])

Fit the stacking ensemble model

get_params([deep])

Get parameters for this estimator.

predict(X)

Calculate the prediction of the ensemble for a given set of date / time

set_params(**params)

Set the parameters of this estimator.

Methods Documentation

fit(X, y=None)[source]

Fit the stacking ensemble model

Parameters
Returns

A fitted StackingEnsemble instance

Return type

StackingEnsemble

get_params(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 – Parameter names mapped to their values.

Return type

dict

predict(X)[source]

Calculate the prediction of the ensemble for a given set of date / time

Parameters

X (pandas.DataFrame) – Input features.

Returns

A DataFrame container with the index being the input (date)time vector. The single column in the DataFrame contains the prediction and the column name is the name of the model (i.e. the name parameter passed to the constructor)

Return type

pandas.DataFrame

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). 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 – Estimator instance.

Return type

estimator instance