Skip to content

PolynomialFeatures

yohou.preprocessing.sklearn_wrappers.PolynomialFeatures

Bases: SklearnTransformer

Generate polynomial and interaction features.

Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree.

For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].

This is a Yohou wrapper that preserves the polars DataFrame structure and "time" column.

Parameters

Name Type Description Default
degree int or tuple(min_degree, max_degree)

If a single int is given, it specifies the maximal degree of the polynomial features. If a tuple (min_degree, max_degree) is passed, then min_degree is the minimum and max_degree is the maximum polynomial degree of the generated features.

2
interaction_only bool

If True, only interaction features are produced: features that are products of at most degree distinct input features.

False
include_bias bool

If True, then include a bias column, the feature in which all polynomial powers are zero.

True
order ('C', 'F')

Order of output array in the dense case. 'F' order is faster to compute, but may slow down subsequent estimators.

'C'

Attributes

Name Type Description
instance_ PolynomialFeatures

The fitted sklearn PolynomialFeatures instance.

n_features_in_ int

Number of features seen during fit.

n_output_features_ int

The total number of polynomial output features.

Examples

>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import PolynomialFeatures
>>> X = pl.DataFrame({
...     "time": [datetime(2024, 1, i) for i in range(1, 4)],
...     "a": [1.0, 2.0, 3.0],
...     "b": [2.0, 3.0, 4.0],
... })
>>> poly = PolynomialFeatures(degree=2, include_bias=False)
>>> poly.fit(X)
PolynomialFeatures(...)
>>> X_poly = poly.transform(X)
>>> # Generates: a, b, a^2, a*b, b^2
>>> len(X_poly.columns) > len(X.columns)
True

See Also

Source Code

Show/Hide source
class PolynomialFeatures(SklearnTransformer):
    """Generate polynomial and interaction features.

    Generate a new feature matrix consisting of all polynomial combinations of
    the features with degree less than or equal to the specified degree.

    For example, if an input sample is two dimensional and of the form
    ``[a, b]``, the degree-2 polynomial features are ``[1, a, b, a^2, ab, b^2]``.

    This is a Yohou wrapper that preserves the polars DataFrame structure and
    "time" column.

    Parameters
    ----------
    degree : int or tuple (min_degree, max_degree), default=2
        If a single int is given, it specifies the maximal degree of the
        polynomial features. If a tuple ``(min_degree, max_degree)`` is passed,
        then ``min_degree`` is the minimum and ``max_degree`` is the maximum
        polynomial degree of the generated features.

    interaction_only : bool, default=False
        If True, only interaction features are produced: features that are
        products of at most ``degree`` *distinct* input features.

    include_bias : bool, default=True
        If True, then include a bias column, the feature in which all
        polynomial powers are zero.

    order : {'C', 'F'}, default='C'
        Order of output array in the dense case. 'F' order is faster to compute,
        but may slow down subsequent estimators.

    Attributes
    ----------
    instance_ : sklearn.preprocessing.PolynomialFeatures
        The fitted sklearn PolynomialFeatures instance.

    n_features_in_ : int
        Number of features seen during fit.

    n_output_features_ : int
        The total number of polynomial output features.

    Examples
    --------
    >>> import polars as pl
    >>> from datetime import datetime
    >>> from yohou.preprocessing import PolynomialFeatures
    >>> X = pl.DataFrame({
    ...     "time": [datetime(2024, 1, i) for i in range(1, 4)],
    ...     "a": [1.0, 2.0, 3.0],
    ...     "b": [2.0, 3.0, 4.0],
    ... })
    >>> poly = PolynomialFeatures(degree=2, include_bias=False)
    >>> poly.fit(X)  # doctest: +ELLIPSIS
    PolynomialFeatures(...)
    >>> X_poly = poly.transform(X)
    >>> # Generates: a, b, a^2, a*b, b^2
    >>> len(X_poly.columns) > len(X.columns)
    True

    See Also
    --------
    - [`SplineTransformer`][yohou.preprocessing.sklearn_wrappers.SplineTransformer] : Generate spline basis features.

    """

    _estimator_default_class = sklearn_PolynomialFeatures

    def __init__(self, degree=2, interaction_only=False, include_bias=True, order="C", **kwargs):
        super().__init__(
            degree=degree, interaction_only=interaction_only, include_bias=include_bias, order=order, **kwargs
        )

    @property
    def n_output_features_(self) -> int:
        """The total number of polynomial output features."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.n_output_features_

    @property
    def powers_(self) -> np.ndarray:
        """Exponent for each of the inputs in the output."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.powers_

Methods

n_output_features_ property

The total number of polynomial output features.

powers_ property

Exponent for each of the inputs in the output.

Tutorials

The following example notebooks use this component:

  • How to Use Scikit-learn Scalers


    Data-Features

    Wrap sklearn scalers (StandardScaler, MinMaxScaler, RobustScaler, PowerTransformer, PolynomialFeatures) for polars DataFrames with inverse transforms.

    View · Open in marimo