Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn PolynomialFeatures

From Leeroopedia


Knowledge Sources
Domains Feature Engineering, Data Preprocessing
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for generating polynomial and interaction features provided by scikit-learn.

Description

PolynomialFeatures generates 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 module also includes SplineTransformer for B-spline basis functions.

Usage

Use PolynomialFeatures when you want to add non-linearity to a linear model by generating polynomial and interaction terms from existing features. This is particularly useful for polynomial regression and for capturing feature interactions without manually creating them.

Code Reference

Source Location

Signature

class PolynomialFeatures(TransformerMixin, BaseEstimator):
    def __init__(
        self, degree=2, *, interaction_only=False, include_bias=True, order="C"
    ):

Import

from sklearn.preprocessing import PolynomialFeatures

I/O Contract

Inputs

Name Type Required Description
degree int or tuple (min_degree, max_degree) No Maximum degree of the polynomial features. Default is 2.
interaction_only bool No If True, only interaction features are produced (no powers of single features). Default is False.
include_bias bool No If True, include a bias column of ones. Default is True.
order str No Order of output array in the dense case. 'F' for Fortran, 'C' for C order. Default is 'C'.

Outputs

Name Type Description
X_transformed ndarray or sparse matrix of shape (n_samples, n_output_features) The matrix of polynomial features.
n_output_features_ int The total number of output features after polynomial expansion.
powers_ ndarray of shape (n_output_features, n_features) The exponent for each of the inputs in the output.

Usage Examples

Basic Usage

from sklearn.preprocessing import PolynomialFeatures
import numpy as np

X = np.array([[1, 2], [3, 4]])
poly = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly.fit_transform(X)
print(X_poly)
# [[1. 2. 1. 2. 4.]
#  [3. 4. 9. 12. 16.]]
print(poly.get_feature_names_out())
# ['x0', 'x1', 'x0^2', 'x0 x1', 'x1^2']

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment