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 |
Project numeric columns onto their principal components.
Usage
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.
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 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.
num_comp: int = 5Number 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 = 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"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.
The two correlated columns are replaced by two principal components:
| 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:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_pca(select(role='feature', type=NumericType)) [untrained]
fit() learns the component loadings:
Recipe (trained)
Inputs: 2 columns (2 features)
Steps:
step_pca(x, y → PC1, PC2) [trained]
transform() replaces the columns with their scores:
| 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:
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 keeps the source columns alongside the components: