Skip to main content

StepDropCorr

Remove numeric columns whose pairwise absolute correlation is too high.

Usage

Source

StepDropCorr(
    columns,
    *,
    threshold=0.9,
    method="pearson",
    id=None,
)

The selected columns that exceed threshold are dropped, as are columns with no usable correlation at all (constant or all-null), and no new columns are produced. To drop columns with no variation rather than correlated ones, see StepDropZeroVariance and StepDropNearZeroVariance.

See High Correlation Filter 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.

Parameters

columns: Selector

Columns to consider for removal. 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.

threshold: float = 0.9

Pairs whose absolute correlation is strictly above this value trigger a removal. Default 0.9.

method: Literal["pearson", "spearman"] = "pearson"

Correlation method: "pearson" (default) or "spearman".

id: str | None = None
Stable step identifier. Defaults to "corr"; a second step of this type in the same recipe becomes "corr_2", and passing an id already used in the recipe is an error.

Examples

a and b are almost perfectly correlated, so one of them is dropped; the uncorrelated c is kept:

df = pd.DataFrame(
    {
        "a": [1.0, 2.0, 3.0, 4.0],
        "b": [1.0, 2.0, 3.0, 4.1],
        "c": [4.0, 1.0, 3.0, 2.0],
    }
)
df
a b c
0 1.0 1.0 4.0
1 2.0 2.0 1.0
2 3.0 3.0 3.0
3 4.0 4.1 2.0

Specifying the step touches no data:

recipe = Recipe().add(StepDropCorr(s.numeric_features(), threshold=0.9))
recipe
Recipe (untrained)
  Inputs: no roles assigned
  Steps:
    step_drop_corr(select(role='feature', type=NumericType))  [untrained]

fit() computes the correlation matrix and picks the columns to drop:

fitted = recipe.fit(df)
fitted
Recipe (trained)
  Inputs: 2 columns (2 features)
  Steps:
    step_drop_corr(a, b, c)  [trained]

transform() drops them:

fitted.transform(df)
b c
0 1.0 4.0
1 2.0 1.0
2 3.0 3.0
3 4.1 2.0

A threshold above their correlation keeps both:

recipe = Recipe().add(StepDropCorr(s.numeric_features(), threshold=0.9999))
fitted = recipe.fit(df)
fitted.transform(df)
a b c
0 1.0 1.0 4.0
1 2.0 2.0 1.0
2 3.0 3.0 3.0
3 4.0 4.1 2.0