TermList#

class pygam.terms.TermList(*terms, **kwargs)[source]#

Bases: Core, MetaTermMixin

Creates an instance of a TermList.

If duplicate terms are supplied, only the first instance will be kept.

Parameters:
*termslist of terms to
verbosebool

whether to show warnings

Attributes:
n_coefsint

Total number of coefficients contributed by the terms in the model.

hasconstraintbool

bool, whether the term has any constraints.

infodict

Get information about the terms in the term list.

Methods

build_columns(X[, term, verbose])

Construct the model matrix columns for the term.

build_constraints(coefs, 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 TermList instance from a dict.

build_penalties()

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_coef_indices([i])

Get the indices for the coefficients of a term in the term list.

get_params([deep])

Returns a dict of all of the object's user-facing parameters.

pop([i])

Remove the ith term from the term list.

set_params([deep, force])

Sets an object's parameters.

build_columns(X, term=-1, 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(coefs, 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 TermList instance from a dict.

Parameters:
clsclass
infodict

contains all information needed to build the term

build_penalties()[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 array 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_coef_indices(i=-1)[source]#

Get the indices for the coefficients of a term in the term list.

Parameters:
iint

by default int=-1, meaning that coefficient indices are returned for all terms in the term list

Returns:
list of integers
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
pop(i=None)[source]#

Remove the ith term from the term list.

Parameters:
iint, optional

term to remove from term list

by default the last term is popped.

Returns:
termTerm
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