generalized_additive_models.Linear#
- class generalized_additive_models.Linear(feature=None, *, penalty=1, by=None, constraint=None)#
A linear term.
Examples
>>> linear = Linear(feature=0) >>> linear.num_coefficients 1
Fitting and transforming a Linear term extracts the relevant column:
>>> import numpy as np >>> X = np.arange(24).reshape(8, 3) >>> linear.fit_transform(X) array([[ 0], [ 3], [ 6], [ 9], [12], [15], [18], [21]])
Linear terms have a standard quadratic (l2) penalty:
>>> linear.penalty_matrix() array([[1.]])
The square root of the penalty matrix is returned, so we must square it to get back the given penalty:
>>> Linear(feature=0, penalty=5).penalty_matrix()**2 array([[5.]])
It also works for pandas DataFrames:
>>> import pandas as pd >>> df = pd.DataFrame({'a':[1, 2, 3], 'b':[4, 5, 6]}) >>> Linear('b').fit_transform(df) array([[4], [5], [6]]) >>> Linear('b', by='a').fit_transform(df) array([[ 4], [10], [18]]) >>> Linear('a', by='b').fit_transform(df) array([[ 4], [10], [18]])
- __init__(feature=None, *, penalty=1, by=None, constraint=None)#
Initialize a Linear term.
- Parameters:
feature (str or int, optional) – The feature name or index associated with the term. The default is None.
penalty (float, optional) – Regularization penalty. The default is 1.
by (str or int, optional) – The feature name or index associated with a multiplicative term. The default is None.
constraint (str or None, optional) – Either None, or increasing or decreasing. The default is None.
Examples
>>> linear_term = Linear(0, penalty=2) >>> linear_term Linear(feature=0, penalty=2) >>> Linear(0, penalty=2) == Linear(0, penalty=2) True >>> Linear(0, penalty=2) == Linear(0, penalty=3) False >>> Linear(0, penalty=2).is_redundant_with_respect_to(Linear(0, penalty=3)) True
Methods
__init__([feature, penalty, by, constraint])Initialize a Linear term.
fit(X)Fit to data.
fit_transform(X[, y])Fit to data, then transform it.
get_metadata_routing()Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
is_redundant_with_respect_to(other)Check if a Term is redundant with respect to another.
Return the penalty matrix for the term.
set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
transform(X)Transform the input.
Attributes
Name of the term.
Number of coefficients for the term.
- fit(X)#
Fit to data.
- Parameters:
X (np.ndarray or pd.DataFrame) – A dataset of shape (num_samples, num_features).
- name = 'linear'#
Name of the term.
- property num_coefficients#
Number of coefficients for the term.
- penalty_matrix()#
Return the penalty matrix for the term.
- transform(X)#
Transform the input.
- Parameters:
X (np.ndarray) – An ndarray with 2 dimensions of shape (n_samples, n_features).
- Returns:
X – An ndarray for the term.
- Return type:
np.ndarray
Examples
>>> linear = Linear(1) >>> X = np.eye(3) >>> linear.fit_transform(X) array([[0.], [1.], [0.]])