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:
BasePluginClass 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.
- class GAMPredict(constant_extrapolation=False)[source]#
Bases:
BasePluginClass for predicting new outputs from a fitted GAM given new input predictors.
- __init__(constant_extrapolation=False)[source]#
Initialize class
- Parameters:
constant_extrapolation (
bool) – If True, predictor values outside the range of those used to fit the GAM will be predicted using constant extrapolation (i.e. the nearest boundary value). If False, extrapolation extends the trend of each GAM term beyond the range of the training data. Default is False.
- _abc_impl = <_abc._abc_data object>#
- static clip_predictors(gam, predictors)[source]#
Clip predictor values to be within the minimum and maximum values in the training data used to fit the GAM. This enables constant extrapolation when predicting new values.
- 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. The array is modified in place.
- 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:
- 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.