generalized_additive_models.TermList#

class generalized_additive_models.TermList(data=())#
__init__(data=())#

A list of unique terms.

Parameters:
  • data (TYPE, optional) – DESCRIPTION. The default is ().

  • / (TYPE) – DESCRIPTION.

Return type:

None.

Examples

>>> terms = TermList([Linear(0), Intercept()])
>>> terms
TermList([Linear(feature=0), Intercept()])
>>> TermList(Intercept())
TermList([Intercept()])
>>> print(terms)
Linear(feature=0) + Intercept()

Adding terms also produces a TermList.

>>> Intercept() + Linear(0)
TermList([Intercept(), Linear(feature=0)])
>>> Intercept() + Linear(0) + Linear(1)
TermList([Intercept(), Linear(feature=0), Linear(feature=1)])

The TermList behaves very much like a list

>>> terms = TermList([Linear(0), Intercept()])
>>> terms += Spline(1)
>>> terms
TermList([Linear(feature=0), Intercept(), Spline(feature=1)])
>>> terms[:2]
TermList([Linear(feature=0), Intercept()])
>>> terms[0] = 1
Traceback (most recent call last):
 ...
TypeError: Only terms can be added to TermList, not 1

Calling .transform() on a TermList will transform each Term in turn.

>>> X = np.tile(np.arange(10), reps=(2, 1)).T
>>> terms = Intercept() + Linear(0) + Spline(1, degree=0, num_splines=2)
>>> terms.fit_transform(X)
array([[ 1. ,  0. ,  0.5, -0.5],
       [ 1. ,  1. ,  0.5, -0.5],
       [ 1. ,  2. ,  0.5, -0.5],
       [ 1. ,  3. ,  0.5, -0.5],
       [ 1. ,  4. ,  0.5, -0.5],
       [ 1. ,  5. , -0.5,  0.5],
       [ 1. ,  6. , -0.5,  0.5],
       [ 1. ,  7. , -0.5,  0.5],
       [ 1. ,  8. , -0.5,  0.5],
       [ 1. ,  9. , -0.5,  0.5]])

Methods

__init__([data])

A list of unique terms.

append(item, /)

S.append(value) -- append value to the end of the sequence

clear()

copy()

count(value)

extend(other)

S.extend(iterable) -- extend sequence by appending elements from the iterable

fit(X)

Fit to data.

fit_transform(X)

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

index(value, [start, [stop]])

Raises ValueError if the value is not present.

insert(i, item)

S.insert(index, value) -- insert value before index

penalty_matrix()

Return the penalty matrix for the terms.

pop([index])

Raise IndexError if list is empty or index is out of range.

remove(item)

S.remove(value) -- remove first occurrence of value.

reverse()

S.reverse() -- reverse IN PLACE

set_params(**params)

Set the parameters of this estimator.

sort(*args, **kwds)

transform(X)

Transform the input.

Attributes

coef_

num_coefficients

Number of coefficients for the terms.

append(item, /)#

S.append(value) – append value to the end of the sequence

property coef_#
fit(X)#

Fit to data.

Parameters:

X (np.ndarray or pd.DataFrame) – A dataset of shape (num_samples, num_features).

Examples

>>> terms = TermList([Linear(0), Intercept()])
>>> X = np.arange(6).reshape(-1, 1)
>>> terms = terms.fit(X)
>>> terms.num_coefficients
2
>>> terms.transform(X)
array([[0., 1.],
       [1., 1.],
       [2., 1.],
       [3., 1.],
       [4., 1.],
       [5., 1.]])
fit_transform(X)#
get_params(deep=True)#

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

Examples

>>> terms = TermList([Linear(0), Intercept()])
>>> params_shallow = terms.get_params(deep=False)
>>> params_shallow
{'data': [Linear(feature=0), Intercept()]}
>>> TermList(**params_shallow)
TermList([Linear(feature=0), Intercept()])
>>> params_deep = terms.get_params(deep=True)
>>> params_deep
{'0__by': None, '0__constraint': None, '0__feature': 0, '0__penalty': 1, '0': Linear(feature=0), '1': Intercept()}
property num_coefficients#

Number of coefficients for the terms.

penalty_matrix()#

Return the penalty matrix for the terms.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

Examples

Setting with a shallow copy:

>>> terms = TermList([Linear(0), Intercept()])
>>> params_shallow = terms.get_params(deep=False)
>>> params_shallow
{'data': [Linear(feature=0), Intercept()]}
>>> params_changed = {'data': [Linear(feature=1, by=2), Intercept()]}
>>> new_terms = terms.set_params(**params_changed)
>>> new_terms
TermList([Linear(by=2, feature=1), Intercept()])
>>> terms
TermList([Linear(by=2, feature=1), Intercept()])

Setting with a deep copy:

>>> terms = TermList([Linear(0), Intercept()])
>>> params_deep = terms.get_params(deep=True)
>>> params_deep
{'0__by': None, '0__constraint': None, '0__feature': 0, '0__penalty': 1, '0': Linear(feature=0), '1': Intercept()}
>>> params_new = {'0__by': None, '0__feature': 2, '0__penalty': 2, '0': Linear(feature=0), '1': Intercept()}
>>> new_terms = terms.set_params(**params_new)
>>> new_terms
TermList([Linear(feature=2, penalty=2), Intercept()])
>>> terms
TermList([Linear(feature=2, penalty=2), Intercept()])

A TermList may be cloned:

>>> from sklearn.base import clone
>>> terms = TermList([Linear(0, by=2, penalty=9), Intercept()])
>>> clone(terms)
TermList([Linear(by=2, feature=0, penalty=9), Intercept()])
transform(X)#

Transform the input.