# Terms¶

class pygam.terms.Term(feature, lam=0.6, dtype='numerical', fit_linear=False, fit_splines=True, penalties='auto', constraints=None, verbose=False)

Bases: pygam.core.Core

build_columns(X, verbose=False)

construct the model matrix columns for the term

Parameters: X (array-like) – Input dataset with n rows verbose (bool) – whether to show warnings scipy sparse array with n rows
build_constraints(coef, constraint_lam, constraint_l2)

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: coefs (array-like containing the coefficients of a term) – constraint_lam (float,) – penalty to impose on the constraint. typically this is a very large number. constraint_l2 (float,) – loading to improve the numerical conditioning of the constraint matrix. typically this is a very small number. C sparse CSC matrix containing the model constraints in quadratic form
classmethod build_from_info(info)

build a Term instance from a dict

Parameters: cls (class) – info (dict) – contains all information needed to build the term Term instance
build_penalties(verbose=False)

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 – P sparse CSC matrix containing the model penalties in quadratic form
compile(X, verbose=False)

method to validate and prepare data-dependent parameters

Parameters: X (array-like) – Input dataset verbose (bool) – whether to show warnings None
hasconstraint

bool, whether the term has any constraints

info

Returns: dict containing information to duplicate this term
isintercept
istensor
n_coefs

Number of coefficients contributed by the term to the model

class pygam.terms.LinearTerm(feature, lam=0.6, penalties='auto', verbose=False)
build_columns(X, verbose=False)

construct the model matrix columns for the term

Parameters: X (array-like) – Input dataset with n rows verbose (bool) – whether to show warnings scipy sparse array with n rows
build_constraints(coef, constraint_lam, constraint_l2)

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: coefs (array-like containing the coefficients of a term) – constraint_lam (float,) – penalty to impose on the constraint. typically this is a very large number. constraint_l2 (float,) – loading to improve the numerical conditioning of the constraint matrix. typically this is a very small number. C sparse CSC matrix containing the model constraints in quadratic form
classmethod build_from_info(info)

build a Term instance from a dict

Parameters: cls (class) – info (dict) – contains all information needed to build the term Term instance
build_penalties(verbose=False)

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 – P sparse CSC matrix containing the model penalties in quadratic form
compile(X, verbose=False)

method to validate and prepare data-dependent parameters

Parameters: X (array-like) – Input dataset verbose (bool) – whether to show warnings None
get_params(deep=False)

returns a dict of all of the object’s user-facing parameters

Parameters: deep (boolean, default: False) – when True, also gets non-user-facing paramters dict
hasconstraint

bool, whether the term has any constraints

info

Returns: dict containing information to duplicate this term
isintercept
istensor
n_coefs

Number of coefficients contributed by the term to the model

set_params(deep=False, force=False, **parameters)

sets an object’s paramters

Parameters: deep (boolean, default: False) – when True, also sets non-user-facing paramters force (boolean, default: False) – when True, also sets parameters that the object does not already have **parameters (paramters to set) – self
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)
build_columns(X, verbose=False)

construct the model matrix columns for the term

Parameters: X (array-like) – Input dataset with n rows verbose (bool) – whether to show warnings scipy sparse array with n rows
build_constraints(coef, constraint_lam, constraint_l2)

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: coefs (array-like containing the coefficients of a term) – constraint_lam (float,) – penalty to impose on the constraint. typically this is a very large number. constraint_l2 (float,) – loading to improve the numerical conditioning of the constraint matrix. typically this is a very small number. C sparse CSC matrix containing the model constraints in quadratic form
classmethod build_from_info(info)

build a Term instance from a dict

Parameters: cls (class) – info (dict) – contains all information needed to build the term Term instance
build_penalties(verbose=False)

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 – P sparse CSC matrix containing the model penalties in quadratic form
compile(X, verbose=False)

method to validate and prepare data-dependent parameters

Parameters: X (array-like) – Input dataset verbose (bool) – whether to show warnings None
get_params(deep=False)

returns a dict of all of the object’s user-facing parameters

Parameters: deep (boolean, default: False) – when True, also gets non-user-facing paramters dict
hasconstraint

bool, whether the term has any constraints

info

Returns: dict containing information to duplicate this term
isintercept
istensor
n_coefs

Number of coefficients contributed by the term to the model

set_params(deep=False, force=False, **parameters)

sets an object’s paramters

Parameters: deep (boolean, default: False) – when True, also sets non-user-facing paramters force (boolean, default: False) – when True, also sets parameters that the object does not already have **parameters (paramters to set) – self
class pygam.terms.FactorTerm(feature, lam=0.6, penalties='auto', coding='one-hot', verbose=False)
build_columns(X, verbose=False)

construct the model matrix columns for the term

Parameters: X (array-like) – Input dataset with n rows verbose (bool) – whether to show warnings scipy sparse array with n rows
build_constraints(coef, constraint_lam, constraint_l2)

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: coefs (array-like containing the coefficients of a term) – constraint_lam (float,) – penalty to impose on the constraint. typically this is a very large number. constraint_l2 (float,) – loading to improve the numerical conditioning of the constraint matrix. typically this is a very small number. C sparse CSC matrix containing the model constraints in quadratic form
classmethod build_from_info(info)

build a Term instance from a dict

Parameters: cls (class) – info (dict) – contains all information needed to build the term Term instance
build_penalties(verbose=False)

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 – P sparse CSC matrix containing the model penalties in quadratic form
compile(X, verbose=False)

method to validate and prepare data-dependent parameters

Parameters: X (array-like) – Input dataset verbose (bool) – whether to show warnings None
get_params(deep=False)

returns a dict of all of the object’s user-facing parameters

Parameters: deep (boolean, default: False) – when True, also gets non-user-facing paramters dict
hasconstraint

bool, whether the term has any constraints

info

Returns: dict containing information to duplicate this term
isintercept
istensor
n_coefs

Number of coefficients contributed by the term to the model

set_params(deep=False, force=False, **parameters)

sets an object’s paramters

Parameters: deep (boolean, default: False) – when True, also sets non-user-facing paramters force (boolean, default: False) – when True, also sets parameters that the object does not already have **parameters (paramters to set) – self
class pygam.terms.TensorTerm(*args, **kwargs)

Bases: pygam.terms.SplineTerm, pygam.terms.MetaTermMixin

build_columns(X, verbose=False)

construct the model matrix columns for the term

Parameters: X (array-like) – Input dataset with n rows verbose (bool) – whether to show warnings scipy sparse array with n rows
build_constraints(coef, constraint_lam, constraint_l2)

builds the GAM block-diagonal constraint matrix in quadratic form out of constraint matrices specified for each feature.

Parameters: coefs (array-like containing the coefficients of a term) – constraint_lam (float,) – penalty to impose on the constraint. typically this is a very large number. constraint_l2 (float,) – loading to improve the numerical conditioning of the constraint matrix. typically this is a very small number. C sparse CSC matrix containing the model constraints in quadratic form
classmethod build_from_info(info)

build a TensorTerm instance from a dict

Parameters: cls (class) – info (dict) – contains all information needed to build the term TensorTerm instance
build_penalties()

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 – P sparse CSC matrix containing the model penalties in quadratic form
compile(X, verbose=False)

method to validate and prepare data-dependent parameters

Parameters: X (array-like) – Input dataset verbose (bool) – whether to show warnings None
get_params(deep=False)

returns a dict of all of the object’s user-facing parameters

Parameters: deep (boolean, default: False) – when True, also gets non-user-facing paramters dict
hasconstraint

bool, whether the term has any constraints

info

Returns: dict containing information to duplicate this term
isintercept
istensor
n_coefs

Number of coefficients contributed by the term to the model

set_params(deep=False, force=False, **parameters)

sets an object’s paramters

Parameters: deep (boolean, default: False) – when True, also sets non-user-facing paramters force (boolean, default: False) – when True, also sets parameters that the object does not already have **parameters (paramters to set) – self
class pygam.terms.TermList(*terms, **kwargs)

Bases: pygam.core.Core, pygam.terms.MetaTermMixin

build_columns(X, term=-1, verbose=False)

construct the model matrix columns for the term

Parameters: X (array-like) – Input dataset with n rows verbose (bool) – whether to show warnings scipy sparse array with n rows
build_constraints(coefs, constraint_lam, constraint_l2)

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: coefs (array-like containing the coefficients of a term) – constraint_lam (float,) – penalty to impose on the constraint. typically this is a very large number. constraint_l2 (float,) – loading to improve the numerical conditioning of the constraint matrix. typically this is a very small number. C sparse CSC matrix containing the model constraints in quadratic form
classmethod build_from_info(info)

build a TermList instance from a dict

Parameters: cls (class) – info (dict) – contains all information needed to build the term TermList instance
build_penalties()

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 – P sparse CSC matrix containing the model penalties in quadratic form
compile(X, verbose=False)

method to validate and prepare data-dependent parameters

Parameters: X (array-like) – Input dataset verbose (bool) – whether to show warnings None
get_coef_indices(i=-1)

get the indices for the coefficients of a term in the term list

Parameters: i (int) – by default int=-1, meaning that coefficient indices are returned for all terms in the term list list of integers
get_params(deep=False)

returns a dict of all of the object’s user-facing parameters

Parameters: deep (boolean, default: False) – when True, also gets non-user-facing paramters dict
hasconstraint

bool, whether the term has any constraints

info

get information about the terms in the term list

Returns: dict containing information to duplicate the term list
n_coefs

Total number of coefficients contributed by the terms in the model

pop(i=None)

remove the ith term from the term list

Parameters: i (int, optional) – term to remove from term list by default the last term is popped. term Term
set_params(deep=False, force=False, **parameters)

sets an object’s paramters

Parameters: deep (boolean, default: False) – when True, also sets non-user-facing paramters force (boolean, default: False) – when True, also sets parameters that the object does not already have **parameters (paramters to set) – self