Skip to main content

StepPolynomial

Replace numeric columns with their orthogonal polynomial expansion.

Usage

Source

StepPolynomial(
    columns, *, degree=2, keep_original_cols=False, role="feature", id=None
)

Each selected numeric column x is replaced by up to degree orthogonal polynomial columns {x}_1, {x}_2, …, {x}_degree. The orthogonal basis is a centering mean plus a QR transform matrix. For a more local, flexible fit, see StepSplineNatural.

The effective degree per column is capped at n_unique_non_null - 1: a column with k unique values can support at most k - 1 orthogonal polynomial terms. Constant (or all-null) columns get an effective degree of 0; transform() leaves those columns untouched and emits a warning. You can get rid of these warnings by adding a StepDropZeroVariance before this step.

See Polynomial Expansion in Feature Engineering A to Z for background on this technique.

See Arguments shared across steps for the arguments this step shares with the others.

Parameters

columns: Selector

Columns to expand. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.numeric_features() (see Selectors for the full set), or a narwhals or polars selector (narwhals.selectors.numeric(), polars.selectors.numeric()). Selectors are resolved against the data frame’s schema when the recipe is fitted, so the same selector works on any backend. Only numeric columns are meaningful here.

degree: int = 2

Maximum polynomial degree. Must be a positive integer. Default 2. Each column’s effective degree is the smaller of degree and n_unique_non_null - 1.

keep_original_cols: bool = False

If True, the source columns are kept alongside their polynomial terms instead of being dropped. Default False.

role: str | None = "feature"

Role assigned to the new polynomial columns (see Roles). Default "feature".

id: str | None = None
Stable step identifier. Defaults to "polynomial"; a second step of this type in the same recipe becomes "polynomial_2", and passing an id already used in the recipe is an error.

Examples

Each selected column is replaced in place by its orthogonal polynomial terms {x}_1 ... {x}_degree:

df = pd.DataFrame({"x": [1.0, 2.0, 3.0, 4.0]})
df
x
0 1.0
1 2.0
2 3.0
3 4.0

Specifying the step touches no data:

recipe = Recipe().add(StepPolynomial(s.numeric_features(), degree=2))
recipe
Recipe (untrained)
  Inputs: no roles assigned
  Steps:
    step_polynomial(select(role='feature', type=NumericType))  [untrained]

fit() learns the orthogonal basis for each column:

fitted = recipe.fit(df)
fitted
Recipe (trained)
  Inputs: 2 columns (2 features)
  Steps:
    step_polynomial(x → x_1, x_2)  [trained]

transform() replaces the column with its polynomial terms:

fitted.transform(df)
x_1 x_2
0 -0.670820 0.5
1 -0.223607 -0.5
2 0.223607 -0.5
3 0.670820 0.5

A higher degree produces more terms:

recipe = Recipe().add(StepPolynomial(s.numeric_features(), degree=3))
fitted = recipe.fit(df)
fitted.transform(df)
x_1 x_2 x_3
0 -0.670820 0.5 -0.223607
1 -0.223607 -0.5 0.670820
2 0.223607 -0.5 -0.670820
3 0.670820 0.5 0.223607

keep_original_cols keeps the source column alongside its terms:

recipe = Recipe().add(
    StepPolynomial(s.numeric_features(), degree=2, keep_original_cols=True)
)
fitted = recipe.fit(df)
fitted.transform(df)
x x_1 x_2
0 1.0 -0.670820 0.5
1 2.0 -0.223607 -0.5
2 3.0 0.223607 -0.5
3 4.0 0.670820 0.5