openstef.model.regressors package#

Submodules#

openstef.model.regressors.arima module#

This module contains the SARIMAX regressor wrapper around statsmodels implementation.

class openstef.model.regressors.arima.ARIMAOpenstfRegressor(backtest_max_horizon=1440, order=(0, 0, 0), seasonal_order=(0, 0, 0, 0), trend=None)#

Bases: OpenstfRegressor

Wrapper around statmodels implementation of (S)ARIMA(X) model.

The fit of an ARIMA statsmodels produces a result object which is used to perform the various computations around forecasting. (see https://www.statsmodels.org/dev/generated/statsmodels.tsa.arima.model.ARIMAResults.html)

To make a prediction, it needs to update the result object’s historic data, ie the past values of the target/endogenous data and the features/exogenous data, applying the fitted parameters to these new data unrelated to the original training data. This update can be performed by the method update_historic_data.

In the following code, we use interchangeably the statmodels and scikit-learn terminology for the variables:
  • the features ‘x’ is equivalent to the exogenous data: ‘exog’ for short.

  • the target ‘y’ is equivalent to the endogenous data: ‘endog’ for short.

More information here https://www.statsmodels.org/stable/endog_exog.html.

property can_predict_quantiles#

Indicates wether this model can make quantile predictions.

property feature_names#

The names of he features used to train the model.

fit(x, y, **kwargs)#

Fits the regressor.

Parameters:
  • x – Feature matrix

  • y – Labels

  • kwargs – model-specific keywords

Returns:

Fitted model

predict(x, quantile=0.5, **kwargs)#

Makes a prediction. Only available after the model has been trained.

Parameters:
  • x – Feature matrix

  • kwargs – model-specific keywords

Returns:

Prediction

predict_quantile(start, end, exog, quantile)#

Quantile prediction.

It relies on the parameters’ confidence intervals.

Parameters:
  • start (int, str, or datetime, optional) – Zero-indexed observation number at which to start forecasting, i.e., the first forecast is start. Can also be a date string to parse or a datetime type. Default is the the zeroth observation.

  • end (int, str, or datetime, optional) – Zero-indexed observation number at which to end forecasting, i.e., the last forecast is end. Can also be a date string to parse or a datetime type. However, if the dates index does not have a fixed frequency, end must be an integer index if you want out of sample prediction. Default is the last observation in the sample.

  • exog (pd.DataFrame) – Exogenous data (features).

  • quantile (float) – The quantile for the confidence interval.

Returns:

The quantile prediction.

Return type:

pd.Serie

score(x, y)#

Compute R2 score with backtesting strategy.

The backtest is performed by the Time Series cross-validator of scikit-learn which returns first k folds as train set and the (k+1)th fold as test set in the kth split. (see https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html)

It needs to update the historic data with (x_past, y_past) for each split.

set_feature_importance()#

Because report needs ‘weight’ and ‘gain’ as importance metrics, we set the values to these names.

  • ‘weight’ is corresponding to the coefficients values

  • ‘gain’ is corresponding to the pvalue for the nullity test of each coefficient

update_historic_data(x_past, y_past)#

Apply the fitted parameters to new data unrelated to the original training data. It’s a side-effect.

Creates a new result object using the current fitted parameters, applied to a completely new dataset that is assumed to be unrelated to the model’s original data. The new results can then be used for analysis or forecasting. It should be used before forecasting, to wedge the historic data just before the first forecast timestamp, with:

  • New observations from the modeled time-series process.

  • New observations of exogenous regressors.

Parameters:
  • x_past (pd.DataFrame) – The exogenous (features) data.

  • y_past (pd.DataFrame) – The endogenous (target) data.

openstef.model.regressors.custom_regressor module#

This module defines the custom regressor.

class openstef.model.regressors.custom_regressor.CustomOpenstfRegressor#

Bases: OpenstfRegressor

A custom regressor allows to load any custom model that is not included with openSTEF.

abstract classmethod objective()#
Return type:

Type[RegressorObjective]

abstract static valid_kwargs()#
Return type:

list[str]

openstef.model.regressors.custom_regressor.create_custom_objective(custom_model_path)#
openstef.model.regressors.custom_regressor.is_custom_type(model_type)#
openstef.model.regressors.custom_regressor.load_custom_model(custom_model_path)#

Load the external custom model.

Return type:

CustomOpenstfRegressor

openstef.model.regressors.dazls module#

This module defines the DAZL model.

class openstef.model.regressors.dazls.Dazls#

Bases: BaseEstimator

DAZLS model.

The model carries out wind and solar power prediction for unseen target substations using training data from other substations with known components.

Any data-driven model can be plugged and used as the base for the domain and the adaptation model.

For a full reference, see: Teng, S.Y., van Nooten, C. C., van Doorn, J.M., Ottenbros, A., Huijbregts, M., Jansen, J.J. Improving Near Real-Time Predictions of Renewable Electricity Production at Substation Level (Submitted)

fit(features, target)#

Fit the model.

In this function we scale the input of the domain and adaptation models of the DAZLS MODEL. Then we fit the two models. We separate the features into domain_model_input, adaptation_model_input and target, and we use them for the fitting and the training of the models.

Parameters:
  • features – inputs for domain and adaptation model (domain_model_input, adaptation_model_input)

  • target – the expected output (y_train)

predict(x)#

Make a prediction.

For the prediction we use the test data x. We use domain_model_input_columns and adaptation_model_input_columns to separate x in test data for domain model and adaptation model respectively.

Parameters:
  • x (array) – domain_model_test_data, adaptation_model_test_data

  • prediction – The output prediction after both models.

score(truth, prediction)#

Evaluation of the prediction’s output.

Parameters:
  • truth – real values

  • prediction – predicted values

Returns:

RMSE and R2 scores

openstef.model.regressors.lgbm module#

class openstef.model.regressors.lgbm.LGBMOpenstfRegressor(boosting_type='gbdt', num_leaves=31, max_depth=-1, learning_rate=0.1, n_estimators=100, subsample_for_bin=200000, objective=None, class_weight=None, min_split_gain=0.0, min_child_weight=0.001, min_child_samples=20, subsample=1.0, subsample_freq=0, colsample_bytree=1.0, reg_alpha=0.0, reg_lambda=0.0, random_state=None, n_jobs=-1, silent='warn', importance_type='split', **kwargs)#

Bases: LGBMRegressor, OpenstfRegressor

LGBM Regressor which implements the Openstf regressor API.

property can_predict_quantiles#

Attribute that indicates if the model predict particular quantiles.

e.g. XGBQuantileOpenstfRegressor

property feature_names#

Retrieve the model input feature names.

Returns:

The list of feature names

gain_importance_name = 'gain'#
weight_importance_name = 'split'#

openstef.model.regressors.linear module#

This module contains the linear regressor.

class openstef.model.regressors.linear.LinearOpenstfRegressor(missing_values=nan, imputation_strategy=None, fill_value=0)#

Bases: LinearRegressor, OpenstfRegressor

Linear Regressor which implements the Openstf regressor API.

property can_predict_quantiles#

Indicates wether this model can make quantile predictions.

property feature_names#

The names of he features used to train the model.

fit(x, y, **kwargs)#

Fit model.

class openstef.model.regressors.linear.LinearRegressor(missing_values=nan, imputation_strategy=None, fill_value=0)#

Bases: MissingValuesHandler

Linear Regressor wrapped in the metamodel MissingValuesHandler.

This regressor can handle missing values by imputation strategy.

Parameters:
  • missing_values – int, float, str, np.nan or None, default=np.nan The placeholder for the missing values. All occurrences of missing_values will be imputed. For pandas’ dataframes with nullable integer dtypes with missing values, missing_values should be set to np.nan, since pd.NA will be converted to np.nan.

  • imputation_strategy – str, default=None The imputation strategy. - If None no imputation is performed. - If “mean”, then replace missing values using the mean along each column. Can only be used with numeric data. - If “median”, then replace missing values using the median along each column. Can only be used with numeric data. - If “most_frequent”, then replace missing using the most frequent value along each column. Can be used with strings or numeric data. If there is more than one such value, only the smallest is returned. - If “constant”, then replace missing values with fill_value. Can be used with strings or numeric data.

  • fill_value – str or numerical value, default=None When strategy == “constant”, fill_value is used to replace all occurrences of missing_values. If left to the default, fill_value will be 0 when imputing numerical data and “missing_value” for strings or object data types.

openstef.model.regressors.proloaf module#

openstef.model.regressors.regressor module#

class openstef.model.regressors.regressor.OpenstfRegressor#

Bases: BaseEstimator

This class defines the interface to which all ML models within OpenSTEF should adhere.

Required methods are indicated by abstractmethods, for which concrete implementations of ML models should have a definition. Common functionality which is required for the automated pipelines in OpenSTEF is defined in this class.

abstract property can_predict_quantiles: bool#

Attribute that indicates if the model predict particular quantiles.

e.g. XGBQuantileOpenstfRegressor

Return type:

bool

abstract property feature_names: list#

Retrieve the model input feature names.

Return type:

list

Returns:

The list of feature names

abstract fit(x, y, **kwargs)#

Fits the regressor.

Parameters:
  • x (array) – Feature matrix

  • y (array) – Labels

  • kwargs – model-specific keywords

Return type:

RegressorMixin

Returns:

Fitted model

abstract predict(x, **kwargs)#

Makes a prediction. Only available after the model has been trained.

Parameters:
  • x (DataFrame) – Feature matrix

  • kwargs – model-specific keywords

Return type:

array

Returns:

Prediction

score(X, y)#

Makes score method from RegressorMixin available.

set_feature_importance()#

Get feature importance.

Return type:

Optional[DataFrame]

Returns:

DataFrame with feature importance.

openstef.model.regressors.xgb module#

class openstef.model.regressors.xgb.XGBOpenstfRegressor(*, objective='reg:squarederror', **kwargs)#

Bases: XGBRegressor, OpenstfRegressor

XGB Regressor which implements the Openstf regressor API.

property can_predict_quantiles#

Attribute that indicates if the model predict particular quantiles.

e.g. XGBQuantileOpenstfRegressor

property feature_names#

Retrieve the model input feature names.

Returns:

The list of feature names

gain_importance_name = 'total_gain'#
weight_importance_name = 'weight'#

openstef.model.regressors.xgb_quantile module#

class openstef.model.regressors.xgb_quantile.XGBQuantileOpenstfRegressor(quantiles=(0.9, 0.5, 0.1), gamma=0.0, colsample_bytree=1.0, subsample=1.0, min_child_weight=1, max_depth=6, learning_rate=0.3, alpha=0.0, max_delta_step=0)#

Bases: OpenstfRegressor

property can_predict_quantiles#

Attribute that indicates if the model predict particular quantiles.

e.g. XGBQuantileOpenstfRegressor

property feature_names#

Retrieve the model input feature names.

Returns:

The list of feature names

fit(x, y, **kwargs)#

Fits xgb quantile model.

Parameters:
  • x (array) – Feature matrix

  • y (array) – Labels

Return type:

OpenstfRegressor

Returns:

Fitted XGBQuantile model

classmethod get_feature_importances_from_booster(booster)#

Gets feauture importances from a XGB booster.

This is based on the feature_importance_ property defined in: dmlc/xgboost.

Parameters:

booster (Booster) – Booster object, most of the times the median model (quantile=0.5) is preferred

Return type:

ndarray

Returns:

Ndarray with normalized feature importances.

predict(x, quantile=0.5)#

Makes a prediction for a desired quantile.

Parameters:
  • x (array) – Feature matrix

  • quantile (float) – Quantile for which a prediciton is desired, note that only quantile are available for which a model is trained, and that this is a quantile-model specific keyword

Return type:

array

Returns:

Prediction

Raises:

ValueError in case no model is trained for the requested quantile

Module contents#