Polynomial feature construction.
This module provides polynomial and interaction feature generation.
Classes
class features.generation.polynomial.PolynomialFeaturesGenerator(FeatureConstructor)
Generate polynomial and interaction features.
Creates a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree.
Constructor
__init__( self, degree: int = 2, interaction_only: bool = False, include_bias: bool = True, order: str = 'C', )
Overview
Polynomial features are used to model non-linear relationships by including higher-order terms and interactions between features.
If an input sample is [a, b], the degree-2 polynomial features are: [1, a, b, a^2, ab, b^2].
Theory
For a set of variables X_1, X_2, \dots, X_p, a polynomial feature of degree d is given by:
X_1^{e_1} X_2^{e_2} \dots X_p^{e_p} \quad \text{where} \quad \sum_{i=1}^p e_i \le d
Parameters
degree
int
= 2
Maximum degree of the polynomial features.
interaction_only
bool
= False
If True, only interaction features are produced: features that are products of at most
degree distinct input features (e.g., :math:`ab` is kept, but :math:`a^2` is not).
include_bias
bool
= True
If True, include a bias column (all 1s), which corresponds to the degree 0 polynomial.
order
{"C", "F"}
= "C"
Order of output array in the dense case. 'F' order is faster to compute but may slow down subsequent estimators.
Attributes
n_input_features_
int
Number of input features.
n_output_features_
int
Number of output features.
powers_
np.ndarray of shape (n_output_features, n_input_features)
Exponent for each input feature in each output feature.
Notes
Complexity:
- The number of features grows combinatorially with the degree:
Generate degree-2 features:
python
>>> from tuiml.features.generation import PolynomialFeaturesGenerator
>>> import numpy as np
>>> X = np.array([[1, 2], [3, 4]])
>>> poly = PolynomialFeaturesGenerator(degree=2)
>>> X_poly = poly.fit_transform(X)
>>> print(poly.get_feature_names_out(['a', 'b']))
['1' 'a' 'b' 'a^2' 'a*b' 'b^2']
Methods
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'PolynomialFeaturesGenerator'
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'PolynomialFeaturesGenerator'
Compute the polynomial feature combinations.
Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used, present for API consistency.
Returns
self
PolynomialFeaturesGenerator
The fitted transformer.
class features.generation.polynomial.InteractionFeaturesGenerator(FeatureConstructor)
Generate pairwise interaction features only.
Creates interaction terms between all unique pairs of features. This is a specialized version of
PolynomialFeaturesGenerator with degree=2 and interaction_only=True.
Constructor
__init__( self, include_original: bool = True, )
Overview
Interaction features allow models to capture dependencies between two different variables. For features X_i and X_j, the interaction term is X_i \cdot X_j.
Parameters
include_original
bool
= True
If True, include original input features in the output matrix.
Attributes
n_input_features_
int
Number of input features.
n_output_features_
int
Number of output features.
interaction_pairs_
list of tuple
Indices of feature pairs used for interactions.
Generate interaction terms for 3 features:
python
>>> from tuiml.features.generation import InteractionFeaturesGenerator
>>> import numpy as np
>>> X = np.array([[1, 2, 3], [4, 5, 6]])
>>> inter = InteractionFeaturesGenerator(include_original=True)
>>> X_inter = inter.fit_transform(X)
>>> print(X_inter.shape[1])
6
Methods
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'InteractionFeaturesGenerator'
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'InteractionFeaturesGenerator'
Compute interaction pairs.
Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used, present for API consistency.
Returns
self
InteractionFeaturesGenerator
The fitted transformer.