FactorTerm#
- class pygam.terms.FactorTerm(feature, lam=0.6, penalties='auto', coding='one-hot', verbose=False)[source]#
Bases:
SplineTermCreates an instance of a FactorTerm.
- Parameters:
- featureint
Index of the feature to use for the feature function.
- lamfloat or iterable of floats
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 lam must be equal to the length of penalties
- penalties{‘auto’, ‘derivative’, ‘l2’, None} or callable or iterable
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 a callable.
- coding{‘one-hot’, ‘dummy’} type of contrast encoding to use.
‘one-hot’ keeps all columns while ‘dummy’ drops the first column.
- 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