PolynomialFeatures#

[1]:
import numpy as np
from sklearn.preprocessing import PolynomialFeatures

X = np.arange(6).reshape(3, 2)
X
[1]:
array([[0, 1],
       [2, 3],
       [4, 5]])
[2]:
polynomialFeatures = PolynomialFeatures(
    # -------------------------------------------------------------------------
    # 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.
    degree=2,
    # -------------------------------------------------------------------------
    # If true, only interaction features are produced: features that are
    # products of at most degree distinct input features, i.e. terms with power
    # of 2 or higher of the same input feature are excluded:
    #
    # - included: x[0], x[1], x[0] * x[1], etc.
    # - excluded: x[0] ** 2, x[0] ** 2 * x[1], etc.
    #
    interaction_only=False,
    # -------------------------------------------------------------------------
    # f True (default), then include a bias column, the feature in which all
    # polynomial powers are zero
    include_bias=True,
)

polynomialFeatures.fit(X)

polynomialFeatures.transform(X)
[2]:
array([[ 1.,  0.,  1.,  0.,  0.,  1.],
       [ 1.,  2.,  3.,  4.,  6.,  9.],
       [ 1.,  4.,  5., 16., 20., 25.]])
[3]:
#
# powers_[i, j] is the exponent of the jth input in the ith output.
#
polynomialFeatures.powers_
[3]:
array([[0, 0],
       [1, 0],
       [0, 1],
       [2, 0],
       [1, 1],
       [0, 2]])