User API

Terms

Linear Term

pygam.terms.l(feature, lam=0.6, penalties='auto', verbose=False)

creates an instance of a LinearTerm

feature : int
Index of the feature to use for the feature function.
lam : float 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.

n_coefs : int
Number of coefficients contributed by the term to the model
istensor : bool
whether the term is a tensor product of sub-terms
isintercept : bool
whether the term is an intercept
hasconstraint : bool
whether the term has any constraints
info : dict
contains dict with the sufficient information to duplicate the term

See also

LinearTerm()
for developer details

Spline Term

pygam.terms.s(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)

creates an instance of a SplineTerm

feature : int
Index of the feature to use for the feature function.
n_splines : int
Number of splines to use for the feature function. Must be non-negative.
spline_order : int
Order of spline to use for the feature function. Must be non-negative.
lam : float 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.

constraints : {None, ‘convex’, ‘concave’, ‘monotonic_inc’, ‘monotonic_dec’}

or callable or iterable

Type of constraint to apply to the term.

If an iterable is used, multiple penalties are applied to the term.

dtype : {‘numerical’, ‘categorical’}
String describing the data-type of the feature.
basis : {‘ps’, ‘cp’}

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_knots : optional, array-like of floats of length 2

these values specify minimum and maximum domain of the spline function.

in the case that spline_basis=”cp”, edge_knots determines the period of the cyclic function.

when edge_knots=None these values are inferred from the data.

default: None

by : int, optional

Feature to use as a by-variable in the term.

For example, if feature = 2 by = 0, then the term will produce: x0 * f(x2)

n_coefs : int
Number of coefficients contributed by the term to the model
istensor : bool
whether the term is a tensor product of sub-terms
isintercept : bool
whether the term is an intercept
hasconstraint : bool
whether the term has any constraints
info : dict
contains dict with the sufficient information to duplicate the term

See also

SplineTerm()
for developer details

Factor Term

pygam.terms.f(feature, lam=0.6, penalties='auto', coding='one-hot', verbose=False)

creates an instance of a FactorTerm

feature : int
Index of the feature to use for the feature function.
lam : float 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’} type of contrast encoding to use.
currently, only ‘one-hot’ encoding has been developed. this means that we fit one coefficient per category.
n_coefs : int
Number of coefficients contributed by the term to the model
istensor : bool
whether the term is a tensor product of sub-terms
isintercept : bool
whether the term is an intercept
hasconstraint : bool
whether the term has any constraints
info : dict
contains dict with the sufficient information to duplicate the term

See also

FactorTerm()
for developer details

Tensor Term

pygam.terms.te(*args, **kwargs)

creates an instance of a TensorTerm

This is useful for creating interactions between features, or other terms.

*args : marginal Terms to combine into a tensor product

feature : list of integers
Indices of the features to use for the marginal terms.
n_splines : list of integers
Number of splines to use for each marginal term. Must be of same length as feature.
spline_order : list of integers
Order of spline to use for the feature function. Must be of same length as feature.
lam : float 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.

constraints : {None, ‘convex’, ‘concave’, ‘monotonic_inc’, ‘monotonic_dec’}

or callable or iterable

Type of constraint to apply to the term.

If an iterable is used, multiple penalties are applied to the term.

dtype : list of {‘numerical’, ‘categorical’}

String describing the data-type of the feature.

Must be of same length as feature.

basis : list of {‘ps’}

Type of basis function to use in the term.

‘ps’ : p-spline basis

NotImplemented: ‘cp’ : cyclic p-spline basis

Must be of same length as feature.

by : int, optional

Feature to use as a by-variable in the term.

For example, if feature = [1, 2] by = 0, then the term will produce: x0 * te(x1, x2)

n_coefs : int
Number of coefficients contributed by the term to the model
istensor : bool
whether the term is a tensor product of sub-terms
isintercept : bool
whether the term is an intercept
hasconstraint : bool
whether the term has any constraints
info : dict
contains dict with the sufficient information to duplicate the term

See also

TensorTerm()
for developer details