## StepSplineNatural


Replace numeric columns with a natural cubic spline basis expansion.


Usage

``` python
StepSplineNatural(
    columns,
    *,
    deg_free=10,
    complete_set=False,
    knots=None,
    keep_original_cols=False,
    role="feature",
    id=None
)
```


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](StepSplineMonotone.md#mlfeatures.StepSplineMonotone); for a lower-degree global fit, see [StepPolynomial](StepPolynomial.md#mlfeatures.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()](Step.transform.md#mlfeatures.Step.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](https://feaz-book.com/numeric-splines) in *Feature Engineering A to Z* for background on this technique.

See [Arguments shared across steps](../user-guide/common-step-arguments.md) 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()](Selectors.md#mlfeatures.Selectors.numeric_features) (see [Selectors](Selectors.md#mlfeatures.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 = ``10`  
Number 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 = ``False`  
Whether to keep the constant column of the complete natural spline basis; when `False` it is dropped. Default `False`.

`knots: Sequence[float] | None = None`  
Explicit 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 = ``False`  
If `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](Roles.md#mlfeatures.Roles)). Default `"feature"`.

`id: str | None = None`  
Stable step identifier. Defaults to `"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.


## Examples

Each selected column is replaced in place by its natural-spline basis columns `{x}_1 ... {x}_deg_free`:


``` python
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 |


Specifying the step touches no data:


``` python
recipe = Recipe().add(StepSplineNatural(s.numeric_features(), deg_free=3))
recipe
```


    Recipe (untrained)
      Inputs: no roles assigned
      Steps:
        step_spline_natural(select(role='feature', type=NumericType))  [untrained]


[fit()](Step.fit.md#mlfeatures.Step.fit) places the interior knots at quantiles of the training data:


``` python
fitted = recipe.fit(df)
fitted
```


    Recipe (trained)
      Inputs: 3 columns (3 features)
      Steps:
        step_spline_natural(x → x_1, x_2, x_3)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) replaces the column with its basis columns:


``` python
fitted.transform(df)
```


|     | 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:


``` python
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](Step.keep_original_cols.md#mlfeatures.Step.keep_original_cols) keeps the source column alongside its basis columns:


``` python
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 |
