SplineTerm#
- class pygam.terms.SplineTerm(feature, n_splines=20, spline_order=3, lam=0.6, penalties='auto', constraints=None, dtype='numerical', basis='ps', by=None, edge_knots=None, verbose=False)[source]#
Bases:
TermCreates an instance of a SplineTerm.
- Parameters:
- featureint
Index of the feature to use for the feature function.
- n_splinesint, default=20
Number of splines to use for the feature function.
Must be non-negative.
- spline_orderint, default=3
Order of spline to use for the feature function.
Must be non-negative.
- lamfloat or iterable of floats, default=0.6
Strength of smoothing penalty. Must be a positive float.
Larger values enforce stronger smoothing.
If single value is passed, it will be repeated for every penalty.
If iterable is passed, the length of
lammust be equal to the length ofpenalties- penalties{‘auto’, ‘derivative’, ‘l2’, None} or callable or iterable, default=’auto’
Type of smoothing penalty to apply to the term.
If an iterable is used, multiple penalties are applied to the term. The length of the iterable must match the length of
lam.If
'auto', then 2nd derivative smoothing for'numerical'dtypes, and L2/ridge smoothing for'categorical'dtypes.Custom penalties can be passed as callables.
- constraints{None, ‘convex’, ‘concave’, ‘monotonic_inc’, ‘monotonic_dec’}
or callable or iterable, defualt=None
Type of constraint to apply to the term.
If an iterable is used, multiple penalties are applied to the term.
- dtype{‘numerical’, ‘categorical’}, default=’numerical’
String describing the data-type of the feature.
- basis{‘ps’, ‘cp’}, default=’ps’
Type of basis function to use in the term.
- ps :
p-spline basis
- cp :
Cyclic p-spline basis, useful for building periodic functions.
By default, the maximum and minimum of the feature values are used to determine the function’s period.
To specify a custom period use argument
edge_knots
- edge_knotsoptional, array-like of floats of length 2, default=None
These values specify minimum and maximum domain of the spline function.
In the case that
basis="cp",edge_knotsdetermines the period of the cyclic function.When
edge_knots=Nonethese values are inferred from the data.- byint, optional, default=None
Feature to use as a by-variable in the term.
For example, if
feature= 2by= 0, then the term will produce:>>> x0 * f(x2)
- Attributes:
n_coefsintNumber of coefficients contributed by the term to the model.
- istensorbool
whether the term is a tensor product of sub-terms
- isinterceptbool
whether the term is an intercept
hasconstraintboolbool, whether the term has any constraints.
infodictGet information about this term.
Methods
build_columns(X[, verbose])Construct the model matrix columns for the term.
build_constraints(coef, constraint_lam, ...)Builds the GAM block-diagonal constraint matrix in quadratic form out of constraint matrices specified for each feature.
build_from_info(info)Build a Term instance from a dict.
build_penalties([verbose])Builds the GAM block-diagonal penalty matrix in quadratic form out of penalty matrices specified for each feature.
compile(X[, verbose])Method to validate and prepare data-dependent parameters.
get_params([deep])Returns a dict of all of the object's user-facing parameters.
set_params([deep, force])Sets an object's parameters.
- build_columns(X, verbose=False)[source]#
Construct the model matrix columns for the term.
- Parameters:
- Xarray-like
Input dataset with n rows
- verbosebool
whether to show warnings
- Returns:
- scipy sparse array with n rows
- build_constraints(coef, constraint_lam, constraint_l2)[source]#
Builds the GAM block-diagonal constraint matrix in quadratic form out of constraint matrices specified for each feature.
behaves like a penalty, but with a very large lambda value, ie 1e6.
- Parameters:
- coefsarray-like containing the coefficients of a term
- constraint_lamfloat,
penalty to impose on the constraint.
typically this is a very large number.
- constraint_l2float,
loading to improve the numerical conditioning of the constraint matrix.
typically this is a very small number.
- Returns:
- Csparse CSC matrix containing the model constraints in quadratic form
- classmethod build_from_info(info)[source]#
Build a Term instance from a dict.
- Parameters:
- clsclass
- infodict
contains all information needed to build the term
- build_penalties(verbose=False)[source]#
Builds the GAM block-diagonal penalty matrix in quadratic form out of penalty matrices specified for each feature.
each feature penalty matrix is multiplied by a lambda for that feature.
so for m features: P = block_diag[lam0 * P0, lam1 * P1, lam2 * P2, … , lamm * Pm]
- Parameters:
- None
- Returns:
- Psparse CSC matrix containing the model penalties in quadratic form
- compile(X, verbose=False)[source]#
Method to validate and prepare data-dependent parameters.
- Parameters:
- Xarray-like
Input dataset
- verbosebool
whether to show warnings
- Returns:
- None
- get_params(deep=False)[source]#
Returns a dict of all of the object’s user-facing parameters.
- Parameters:
- deepboolean, default: False
when True, also gets non-user-facing parameters
- Returns:
- dict
- set_params(deep=False, force=False, **parameters)[source]#
Sets an object’s parameters.
- Parameters:
- deepboolean, default: False
when True, also sets non-user-facing parameters
- forceboolean, default: False
when True, also sets parameters that the object does not already have
- **parametersparameters to set
- Returns:
- self