Polynomial regression
Add curvature and cross-feature effects to a linear estimator.
Scikit-Learn · Class
PolynomialFeatures expands input columns into polynomial and interaction terms up to a chosen degree. It supports bounded degree ranges, interaction-only expansion, optional bias, and dense-output memory order.
The transformer enumerates feature combinations whose total polynomial degree falls within the configured range. It can include a constant column, restrict output to products of distinct inputs, and choose the memory layout for dense output; fit_transform learns the input feature structure and produces the expanded matrix.
class sklearn.preprocessing.PolynomialFeatures(degree=2, *, interaction_only=False, include_bias=True, order='C')| Name | Type | Description |
|---|---|---|
degree | int or tuple (min_degree, max_degree) | Maximum degree when an integer is supplied, or minimum and maximum degrees when a tuple is supplied. Default: 2. |
interaction_only | bool | If true, generate only interaction features. Default: False. |
include_bias | bool | If true, include the degree-zero bias column. Default: True. |
order | {'C', 'F'} | Dense output memory order. Default: 'C'. |
A transformed feature matrix containing the generated polynomial and interaction features; the fitted transformer also exposes feature-count and output-feature-name information.
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
X = np.array([[1, 2], [3, 4]])
poly = PolynomialFeatures(degree=2, include_bias=False)
X_expanded = poly.fit_transform(X)
print(X_expanded)
print(poly.get_feature_names_out(["x1", "x2"]))Expands two input columns into linear, squared, and pairwise-product terms without adding a column of ones.
Add curvature and cross-feature effects to a linear estimator.
Represent products among distinct variables without repeated powers.
Build a preprocessing stage before a linear model.
It creates a matrix containing polynomial combinations of the input features up to the requested degree. With two inputs and degree two, the terms include a constant, each original feature, both squares, and their product when the bias is enabled.
Follow-up: How would you omit squared terms while retaining interactions? Set interaction_only=True; this excludes repeated powers of the same input feature while retaining products of distinct features.
degree may be one integer specifying the maximum degree or a tuple specifying minimum and maximum degrees. include_bias controls whether the all-zero-degree constant term is included.
Follow-up: What is the practical distinction between degree=(0, 2) and degree=(1, 2)? The generated degree-zero term is governed by include_bias, so the two minimum-degree choices are equivalent for that term.
interaction_only=True generates products involving at most degree distinct input features. Consequently, terms such as x squared are excluded, while products such as x1 times x2 remain available.
Follow-up: When might this option reduce feature growth? It avoids repeated powers and therefore produces fewer columns than a full polynomial expansion for the same maximum degree.
sklearn.preprocessing.SplineTransformersklearn.preprocessing.Pipelinesklearn.linear_model.LinearRegression