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 monotone (I-spline) basis expansion.
Usage
Each selected numeric column x is replaced by deg_free I-spline basis columns {x}_1, {x}_2, …, {x}_{deg_free}. Every basis column is non-negative and non-decreasing in x; fit with a procedure that keeps its coefficients non-negative (e.g. non-negative least squares), the resulting model is monotonic in x. For an unconstrained flexible fit, see StepSplineNatural.
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 continues the local polynomial piece and can leave [0, 1] or lose monotonicity: the shape guarantee only holds within the training range.
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 at least degree (plus 1 if complete_set). Default 10. Ignored when knots is given.
degree: int = 3Degree of the underlying M-spline; the I-spline’s pieces have degree degree + 1. Must be a non-negative integer. Default 3.
complete_set: bool = TrueWhether to keep the first column of the complete I-spline basis; when False it is dropped. Keeping it adds one to the minimum deg_free. Default True.
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_monotone"; a second step of this type in the same recipe becomes "spline_monotone_2", and passing an id already used in the recipe is an error.
Each selected column is replaced in place by its monotone-spline basis columns {x}_1 ... {x}_deg_free, each rising from 0 to 1:
| 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_monotone(select(role='feature', type=NumericType)) [untrained]
fit() places the interior knots at quantiles of the training data:
Recipe (trained)
Inputs: 4 columns (4 features)
Steps:
step_spline_monotone(x → x_1, x_2, x_3, x_4) [trained]
transform() replaces the column with its basis columns:
| x_1 | x_2 | x_3 | x_4 | |
|---|---|---|---|---|
| 0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 0.460225 | 0.100375 | 0.010412 | 0.000416 |
| 2 | 0.739692 | 0.323199 | 0.073303 | 0.006664 |
| 3 | 0.893378 | 0.573511 | 0.213661 | 0.033736 |
| 4 | 0.966264 | 0.786339 | 0.426489 | 0.106622 |
| 5 | 0.993336 | 0.926697 | 0.676801 | 0.260308 |
| 6 | 0.999584 | 0.989588 | 0.899625 | 0.539775 |
| 7 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
A higher deg_free produces more, more flexible basis columns:
recipe = Recipe().add(StepSplineMonotone(s.numeric_features(), deg_free=6))
fitted = recipe.fit(df)
fitted.transform(df)| x_1 | x_2 | x_3 | x_4 | x_5 | x_6 | |
|---|---|---|---|---|---|---|
| 0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 0.893378 | 0.344388 | 0.042170 | 0.001874 | 0.000000 | 0.000000 |
| 2 | 0.999584 | 0.787172 | 0.254894 | 0.029988 | 0.000000 | 0.000000 |
| 3 | 1.000000 | 0.967461 | 0.585277 | 0.150146 | 0.000833 | 0.000000 |
| 4 | 1.000000 | 0.999167 | 0.849854 | 0.414723 | 0.032539 | 0.000000 |
| 5 | 1.000000 | 1.000000 | 0.970012 | 0.745106 | 0.212828 | 0.000416 |
| 6 | 1.000000 | 1.000000 | 0.998126 | 0.957830 | 0.655612 | 0.106622 |
| 7 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
keep_original_cols keeps the source column alongside its basis columns:
recipe = Recipe().add(
StepSplineMonotone(s.numeric_features(), deg_free=4, keep_original_cols=True)
)
fitted = recipe.fit(df)
fitted.transform(df)| x | x_1 | x_2 | x_3 | x_4 | |
|---|---|---|---|---|---|
| 0 | 1.0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 2.0 | 0.460225 | 0.100375 | 0.010412 | 0.000416 |
| 2 | 3.0 | 0.739692 | 0.323199 | 0.073303 | 0.006664 |
| 3 | 4.0 | 0.893378 | 0.573511 | 0.213661 | 0.033736 |
| 4 | 5.0 | 0.966264 | 0.786339 | 0.426489 | 0.106622 |
| 5 | 6.0 | 0.993336 | 0.926697 | 0.676801 | 0.260308 |
| 6 | 7.0 | 0.999584 | 0.989588 | 0.899625 | 0.539775 |
| 7 | 8.0 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |