df = pd.DataFrame(
{
"x": [1.0, 2.0, 3.0, 4.0],
"y": [2.1, 3.9, 6.1, 8.0],
"target": [1.0, 2.0, 3.0, 4.0],
}
)
df| x | y | target | |
|---|---|---|---|
| 0 | 1.0 | 2.1 | 1.0 |
| 1 | 2.0 | 3.9 | 2.0 |
| 2 | 3.0 | 6.1 | 3.0 |
| 3 | 4.0 | 8.0 | 4.0 |
Project numeric columns onto partial-least-squares components.
Usage
The selected numeric columns are replaced by num_comp component columns {prefix}1 ... {prefix}{num_comp}. Each component is a linear combination of all the input columns chosen to covary with target, so no single component belongs to any one of the input columns. Unlike unsupervised extraction such as StepPca, the components depend on the target as well as the selected columns. Asking for more components than there are input columns yields fewer.
target may be numeric (partial least squares regression) or categorical/string (partial least squares discriminant analysis, fit by dummy-coding the classes into one indicator column per class).
The columns are centered but not scaled, so a column measured on a larger numeric range contributes more to the components; normalize first if that is not what you want.
Rows with a missing value in a selected column or a missing target are excluded when the components are fitted, and a row with a missing selected-column value gets NaN for every component at transform time.
See Partial Least Squares 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 project. 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.
target: strName of the column in the training data frame that holds the numeric or categorical target. Read at fit time only; the column is not declared as an input feature in schema_in().
num_comp: int = 5Number of components to keep. Must be a positive integer. Capped at the number of selected columns and at the number of complete (null-free) training samples, so fewer components are produced when either is smaller than num_comp. Default 5.
prefix: str = "PLS"Prefix for the produced component column names. Default "PLS".
keep_original_cols: bool = FalseIf True, the source columns are kept alongside the components instead of being dropped. Default False.
role: str | None = "feature"Role assigned to the new component columns (see Roles). Default "feature".
id: str | None = None"pls"; a second step of this type in the same recipe becomes "pls_2", and passing an id already used in the recipe is an error.
The two feature columns are replaced by one PLS component predictive of target:
df = pd.DataFrame(
{
"x": [1.0, 2.0, 3.0, 4.0],
"y": [2.1, 3.9, 6.1, 8.0],
"target": [1.0, 2.0, 3.0, 4.0],
}
)
df| x | y | target | |
|---|---|---|---|
| 0 | 1.0 | 2.1 | 1.0 |
| 1 | 2.0 | 3.9 | 2.0 |
| 2 | 3.0 | 6.1 | 3.0 |
| 3 | 4.0 | 8.0 | 4.0 |
Specifying the step touches no data:
recipe = Recipe().add(
StepPls(s.numeric_features() - "target", target="target", num_comp=1)
)
recipeRecipe (untrained)
Inputs: no roles assigned
Steps:
step_pls(select(role='feature', type=NumericType) & ~'target') [untrained]
fit() learns the component rotation:
Recipe (trained)
Inputs: 2 columns (2 features)
Steps:
step_pls(x, y → PLS1) [trained]
transform() replaces the columns with their scores:
keep_original_cols keeps the source columns alongside the components:
recipe = Recipe().add(
StepPls(
s.numeric_features() - "target",
target="target",
num_comp=1,
keep_original_cols=True,
)
)
fitted = recipe.fit(df)
fitted.transform(df)| x | y | target | PLS1 | |
|---|---|---|---|---|
| 0 | 1.0 | 2.1 | 1.0 | -3.287081 |
| 1 | 2.0 | 3.9 | 2.0 | -1.229723 |
| 2 | 3.0 | 6.1 | 3.0 | 1.185046 |
| 3 | 4.0 | 8.0 | 4.0 | 3.331757 |
A categorical target runs partial least squares discriminant analysis instead, by dummy-coding its classes:
df_class = pd.DataFrame(
{
"x": [1.0, 2.0, 3.0, 4.0],
"y": [2.1, 3.9, 6.1, 8.0],
"target": ["a", "a", "b", "b"],
}
)
recipe = Recipe().add(
StepPls(s.numeric_features() - "target", target="target", num_comp=1)
)
fitted = recipe.fit(df_class)
fitted.transform(df_class)| target | PLS1 | |
|---|---|---|
| 0 | a | -3.286814 |
| 1 | a | -1.230099 |
| 2 | b | 1.185268 |
| 3 | b | 3.331645 |