## StepPls


Project numeric columns onto partial-least-squares components.


Usage

``` python
StepPls(
    columns,
    target,
    *,
    num_comp=5,
    prefix="PLS",
    keep_original_cols=False,
    role="feature",
    id=None
)
```


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](StepPca.md#mlfeatures.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](https://feaz-book.com/too-many-pls) 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 project. 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.

`target: str`  
Name 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()](Step.schema_in.md#mlfeatures.Step.schema_in).

`num_comp: int = ``5`  
Number 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 = ``False`  
If `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](Roles.md#mlfeatures.Roles)). Default `"feature"`.

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


## Examples

The two feature columns are replaced by one PLS component predictive of `target`:


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


``` python
recipe = Recipe().add(
    StepPls(s.numeric_features() - "target", target="target", num_comp=1)
)
recipe
```


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


[fit()](Step.fit.md#mlfeatures.Step.fit) learns the component rotation:


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


    Recipe (trained)
      Inputs: 2 columns (2 features)
      Steps:
        step_pls(x, y → PLS1)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) replaces the columns with their scores:


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


|     | target | PLS1      |
|-----|--------|-----------|
| 0   | 1.0    | -3.287081 |
| 1   | 2.0    | -1.229723 |
| 2   | 3.0    | 1.185046  |
| 3   | 4.0    | 3.331757  |


[keep_original_cols](Step.keep_original_cols.md#mlfeatures.Step.keep_original_cols) keeps the source columns alongside the components:


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


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