improver.utilities.generalized_additive_models module#

Module to contain methods for fitting and predicting using generalized additive models.

class GAMFit(model_specification, max_iter=100, tol=0.0001, distribution='normal', link='identity', fit_intercept=True)[source]#

Bases: BasePlugin

Class for fitting Generalized Additive Models (GAMs) which predict the mean or standard deviation of input forecasts or observations.

This class uses functionality from pyGAM (https://pygam.readthedocs.io/en/latest/index.html) to fit the model.

__init__(model_specification, max_iter=100, tol=0.0001, distribution='normal', link='identity', fit_intercept=True)[source]#

Initialize class for fitting GAMs using pyGAM.

Parameters:
  • model_specification (List) –

    A list containing lists of three items (in order):

    1. a string containing a single pyGAM term; one of ‘linear’, ‘spline’, ‘tensor’, or ‘factor’ 2. a list of indices of the features to be included in that term, corresponding to the index of those features in the predictor array 3. a dictionary of kwargs to be included when defining the term

  • max_iter (int) – A pyGAM argument which determines the maximum iterations allowed when fitting the GAM. Defaults to 100.

  • tol (float) – A pyGAM argument determining the tolerance used to define the stopping criteria. Defaults to 0.0001.

  • distribution (str) – A pyGAM argument determining the distribution to be used in the model. The default is a normal distribution.

  • link (str) – A pyGAM argument determining the link function to be used in the model. Defaults to the identity link function, which implies a direct relationship between predictors and target.

  • fit_intercept (bool) – A pyGAM argument determining whether to include an intercept term in the model. Default is True.

_abc_impl = <_abc._abc_data object>#
create_pygam_model()[source]#

Create a GAM model using pyGAM from the model_specification dictionary.

Returns:

GAM model equation constructed using pyGAM model terms.

process(predictors, targets)[source]#

Fit a GAM model using pyGAM.

Parameters:
  • predictors (ndarray) – A 2-D array of predictors. The index of each column (feature) is used in model_specification to determine which feature is included in each model term.

  • targets (ndarray) – A 1-D array of target values associated with the predictors.

Returns:

A fitted pyGAM GAM model.

class GAMPredict[source]#

Bases: BasePlugin

Class for predicting new outputs from a fitted GAM given new input predictors.

__init__()[source]#

Initialize class

_abc_impl = <_abc._abc_data object>#
process(gam, predictors)[source]#

Use pyGAM functionality to predict values from a fitted GAM.

Parameters:
  • gam – A fitted pyGAM GAM model.

  • predictors (ndarray) – A 2-D array of inputs to use to predict new values. Each feature (column) should have the same index as in the training dataset.

Return type:

ndarray

Returns:

A 1-D array of values predicted by the GAM with each value in the array corresponding to one row in the input predictors.