df = pd.DataFrame({"x": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]})
df| x | |
|---|---|
| 0 | 1.0 |
| 1 | 2.0 |
| 2 | 3.0 |
| 3 | 4.0 |
| 4 | 5.0 |
| 5 | 6.0 |
| 6 | 7.0 |
| 7 | 8.0 |
Replace numeric columns with a natural cubic spline basis expansion.
Usage
Each selected numeric column x is replaced by deg_free natural cubic spline basis columns {x}_1, {x}_2, …, {x}_{deg_free}. A natural spline is a piecewise cubic polynomial constrained to be linear beyond its boundary knots, which avoids the wild swings an ordinary polynomial or unconstrained cubic spline can show near the edges of the data. For a basis that keeps a model monotonic in x, see StepSplineMonotone; for a lower-degree global fit, see StepPolynomial.
Interior knots are placed at quantiles of each column’s own training values (or at the caller-supplied knots, shared across every selected column). A column with fewer than 2 distinct training values cannot support a boundary at all; transform() leaves such a column untouched and emits a UserWarning. New data outside the fit-time boundary is extrapolated linearly, not as a wild polynomial continuation: this is what “natural” means for this basis.
See Splines 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.
columns: SelectorColumns 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.
deg_free: int = 10Number of output basis columns per (non-degenerate) selected column. Must be an integer >= 2. The columns include the constant basis column when complete_set is True, which leaves one fewer interior knot. Default 10. Ignored when knots is given.
complete_set: bool = FalseWhether to keep the constant column of the complete natural spline basis; when False it is dropped. Default False.
knots: Sequence[float] | None = NoneExplicit interior knot locations, shared across every selected column, instead of the default quantile placement. Each column’s knots must lie strictly inside that column’s own training boundary. Default None (quantile placement).
keep_original_cols: bool = FalseIf True, the source columns are kept alongside their basis columns instead of being dropped. Default False.
role: str | None = "feature"Role assigned to the new basis columns (see Roles). Default "feature".
id: str | None = None"spline_natural"; a second step of this type in the same recipe becomes "spline_natural_2", and passing an id already used in the recipe is an error.
Each selected column is replaced in place by its natural-spline basis columns {x}_1 ... {x}_deg_free:
| x | |
|---|---|
| 0 | 1.0 |
| 1 | 2.0 |
| 2 | 3.0 |
| 3 | 4.0 |
| 4 | 5.0 |
| 5 | 6.0 |
| 6 | 7.0 |
| 7 | 8.0 |
Specifying the step touches no data:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_spline_natural(select(role='feature', type=NumericType)) [untrained]
fit() places the interior knots at quantiles of the training data:
Recipe (trained)
Inputs: 3 columns (3 features)
Steps:
step_spline_natural(x → x_1, x_2, x_3) [trained]
transform() replaces the column with its basis columns:
| x_1 | x_2 | x_3 | |
|---|---|---|---|
| 0 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 0.301749 | 0.009840 | 0.004373 |
| 2 | 0.485423 | 0.078717 | 0.034985 |
| 3 | 0.447522 | 0.254009 | 0.114189 |
| 4 | 0.254009 | 0.447522 | 0.219145 |
| 5 | 0.078717 | 0.485423 | 0.298348 |
| 6 | 0.009840 | 0.301749 | 0.328960 |
| 7 | 0.000000 | 0.000000 | 0.333333 |
A higher deg_free produces more, more flexible basis columns:
recipe = Recipe().add(StepSplineNatural(s.numeric_features(), deg_free=4))
fitted = recipe.fit(df)
fitted.transform(df)| x_1 | x_2 | x_3 | x_4 | |
|---|---|---|---|---|
| 0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 0.381924 | 0.031098 | 0.000000 | 0.000000 |
| 2 | 0.485787 | 0.246842 | 0.000364 | 0.000162 |
| 3 | 0.254009 | 0.596696 | 0.045554 | 0.020246 |
| 4 | 0.045554 | 0.596696 | 0.254009 | 0.114189 |
| 5 | 0.000364 | 0.246842 | 0.485787 | 0.250891 |
| 6 | 0.000000 | 0.031098 | 0.381924 | 0.322967 |
| 7 | 0.000000 | 0.000000 | 0.000000 | 0.333333 |
keep_original_cols keeps the source column alongside its basis columns:
recipe = Recipe().add(
StepSplineNatural(s.numeric_features(), deg_free=3, keep_original_cols=True)
)
fitted = recipe.fit(df)
fitted.transform(df)| x | x_1 | x_2 | x_3 | |
|---|---|---|---|---|
| 0 | 1.0 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 2.0 | 0.301749 | 0.009840 | 0.004373 |
| 2 | 3.0 | 0.485423 | 0.078717 | 0.034985 |
| 3 | 4.0 | 0.447522 | 0.254009 | 0.114189 |
| 4 | 5.0 | 0.254009 | 0.447522 | 0.219145 |
| 5 | 6.0 | 0.078717 | 0.485423 | 0.298348 |
| 6 | 7.0 | 0.009840 | 0.301749 | 0.328960 |
| 7 | 8.0 | 0.000000 | 0.000000 | 0.333333 |