Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

PolynomialFeatures

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.

How it works

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.

Syntax

class sklearn.preprocessing.PolynomialFeatures(degree=2, *, interaction_only=False, include_bias=True, order='C')

Parameters

NameTypeDescription
degreeint 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_onlyboolIf true, generate only interaction features. Default: False.
include_biasboolIf true, include the degree-zero bias column. Default: True.
order{'C', 'F'}Dense output memory order. Default: 'C'.

Returns

A transformed feature matrix containing the generated polynomial and interaction features; the fitted transformer also exposes feature-count and output-feature-name information.

Key points

  • The transformer maps each row into a feature vector containing combinations up to the configured polynomial degree.
  • An integer degree sets the upper bound; a degree tuple can instead define both the lower and upper bounds.
  • interaction_only keeps products of distinct inputs and removes terms that reuse an input through powers.
  • include_bias adds a constant column of ones, which can represent an intercept term.
  • The order parameter controls the memory layout of dense transformed output and accepts C or F.
  • Output width is determined combinatorially from the number of input features and the selected degree range.

Common mistakes

  • For dense data, column-major output may speed transformation but reduce performance in later estimators.
  • Large polynomial degrees can fit training noise rather than improving generalization.

Example: Expand numeric features to degree two

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.

When to use it

Polynomial regression

Add curvature and cross-feature effects to a linear estimator.

Interaction modeling

Represent products among distinct variables without repeated powers.

Pipeline feature expansion

Build a preprocessing stage before a linear model.

Interview questions

QWhat does PolynomialFeatures do?

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.

QHow do degree and include_bias affect the generated columns?

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.

QWhat does interaction_only change?

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.SplineTransformer
  • sklearn.preprocessing.Pipeline
  • sklearn.linear_model.LinearRegression

Source: PolynomialFeatures — scikit-learn 1.9.1 documentation. Checked 2026-10-15.