## StepPca


Project numeric columns onto their principal components.


Usage

``` python
StepPca(
    columns,
    *,
    num_comp=5,
    prefix="PC",
    keep_original_cols=False,
    role="feature",
    id=None
)
```


The selected numeric columns are replaced by `num_comp` principal-component columns `{prefix}1 ... {prefix}{num_comp}`. Each component is a linear combination of all the input columns, so no single component belongs to any one of them. Asking for more components than there are input columns yields fewer. To choose components that covary with the target instead, see [StepPls](StepPls.md#mlfeatures.StepPls).

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 are excluded when the components are computed, and such a row gets `NaN` for every component.

See [Principal Component Analysis](https://feaz-book.com/too-many-pca) 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.

`num_comp: int = ``5`  
Number of principal 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 = ``"PC"`  
Prefix for the produced component column names. Default `"PC"`.

`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 `"pca"`; a second step of this type in the same recipe becomes `"pca_2"`, and passing an `id` already used in the recipe is an error.


## Examples

The two correlated columns are replaced by two principal components:


``` python
df = pd.DataFrame(
    {"x": [1.0, 2.0, 3.0, 4.0], "y": [2.1, 3.9, 6.1, 8.0]}
)
df
```


|     | x   | y   |
|-----|-----|-----|
| 0   | 1.0 | 2.1 |
| 1   | 2.0 | 3.9 |
| 2   | 3.0 | 6.1 |
| 3   | 4.0 | 8.0 |


Specifying the step touches no data:


``` python
recipe = Recipe().add(StepPca(s.numeric_features(), num_comp=2))
recipe
```


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


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


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


    Recipe (trained)
      Inputs: 2 columns (2 features)
      Steps:
        step_pca(x, y → PC1, PC2)  [trained]


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


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


|     | PC1       | PC2       |
|-----|-----------|-----------|
| 0   | -3.287069 | 0.028375  |
| 1   | -1.229748 | -0.057834 |
| 2   | 1.185062  | 0.035403  |
| 3   | 3.331755  | -0.005944 |


`num_comp` caps how many components are kept:


``` python
recipe = Recipe().add(StepPca(s.numeric_features(), num_comp=1))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | PC1       |
|-----|-----------|
| 0   | -3.287069 |
| 1   | -1.229748 |
| 2   | 1.185062  |
| 3   | 3.331755  |


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


``` python
recipe = Recipe().add(
    StepPca(s.numeric_features(), num_comp=2, keep_original_cols=True)
)
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | x   | y   | PC1       | PC2       |
|-----|-----|-----|-----------|-----------|
| 0   | 1.0 | 2.1 | -3.287069 | 0.028375  |
| 1   | 2.0 | 3.9 | -1.229748 | -0.057834 |
| 2   | 3.0 | 6.1 | 1.185062  | 0.035403  |
| 3   | 4.0 | 8.0 | 3.331755  | -0.005944 |
