te#
- pygam.terms.te(*args, **kwargs)[source]#
Creates an instance of a Tensor Term.
This is useful for creating interactions between features or other terms.
Note
te(...)is preferred overTensorTerm(...)although they are equivalent.
- Parameters:
- *argsTerms or Indices of features to combine into a tensorm product.
For example, we can create a tensor term from marginal spline terms on features 0 and 1:
>>> te(0, 1)
Or we can do it more explicitly by first instantiating the splines on each feature:
>>> te(s(0), s(1))
This representation is useful in order to control specific arguments individual marginal terms:
>>> te(s(0), s(1, n_splines=20))
Note
This is the preferred way to specify marginal terms.
Marginal terms can alternatively be specified via the kwarg
feature=...but not both.- featurelist of Terms or list of integers to use for marginal terms, default=None
If None, then must be specified via
*argsNote
This is not the preferred way to specify marginal terms.
The preferred way is via
*argsThis format exists mostly for symmetry, since it can be easier to align arguments of terms
For example we can build as follows:
>>> te(feature=(0, 1), n_splines=(10,20))
which is equivalent to
>>> te(s(0, n_splines=10), s(1, n_splines=20))
- n_splinesint or list of integers, one per maginal term, default=10
Number of splines to use for each marginal term.
If single value is passed, it will be repeated for every marginal term.
If iterable is passed, it must be of same length as
feature.- spline_orderint or list of integers, one per marginal term, default=3
Order of spline to use for the feature function.
If iterable is passed, it must be of same length as
feature.- lamfloat or iterable of floats, default=0.6
Strength of smoothing penalty. Must be a positive float.
Larger values enforce stronger smoothing.
If a single value is passed, it will be repeated for every penalty.
If an iterable is passed, the length of
lammust equal the length offeatureMultiple smoothing penalties are allowed per merginal term.
In this case, lam should be an iterable of iterables of floats, where the outer length matches the length of
featureand the length of each inner iterable matches the number of penalties per term.- penalties{‘auto’, ‘derivative’, ‘l2’, ‘periodic’, None} or callable, or iterable of these, default=’auto’
Type of smoothing penalty to apply to the term.
If ‘auto’, then 2nd derivative smoothing for ‘numerical’ dtypes, and L2/ridge smoothing for ‘categorical’ dtypes.
If an iterable is passed, the length of
penaltiesmust match the length offeature.Multiple smoothing penalties can be applied to each marginal term.
In this case,
penaltiesshould be an iterable of iterables of penalties. The outer length must match the length offeatureand the length of each inner iterable should match the number of penalties per term.Custom penalties can be passed as callables.
- constraints{None, ‘convex’, ‘concave’, ‘monotonic_inc’, ‘monotonic_dec’} or callable, or iterable of these, default=None
Type of constraint to apply to the term.
If an iterable is passed, the length of
constraintsmust match the length offeature.Multiple constraints can be applied to each term.
In this case, constraints should be an iterable of iterables of constraints. The outer length must match the length of
featureand the length of each inner iterable should match the number of constraints per term.Custom constraints can be passed as callables.
- dtypelist of {‘numerical’, ‘categorical’}
String describing the data-type of the feature.
Must be of same length as
feature.- basislist of {‘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
Must be of same length as
feature.- edge_knotsoptional, array-like of floats of length 2, iterable of array-like, default=None
These values specify minimum and maximum domain of the marginal term spline functions.
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
Feature to use as an overall by-variable in the complete Tensor Term.
For example, if
feature= [1, 2]by= 0, then the term will produce:>>> x0 * te(x1, x2)
In order to apply a by-variable to a marginal term, then it must be added explicitly to the marginal term before building the Tensor Term.
- Attributes:
- n_coefsint
Number 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
- hasconstraintbool
whether the term has any constraints
- infodict
contains dict with the sufficient information to duplicate the term
See also
TensorTermfor developer details
Examples
We can build a tensor term where each marginal term has a by-variable
>>> te(s(0, by=2), s(1, by=3))