Skip to main content

Selectors

Namespace of factory methods for built-in mlfeatures column selectors.

Usage

Source

Selectors()

All factories return a FeaturesSelector that composes with & / | / ~ / -.

Prefer a selector to a hard-coded list of column names. A list goes stale as soon as an earlier step adds or removes columns (a dummy step replaces city with city_LA and city_NYC), while a selector is resolved against the columns each step actually sees, so it keeps working when the data frame changes.

Examples

A selector stands in for an explicit column list and is resolved against the data frame at fit time. Here starts_with("x") picks up x1 and x2 but leaves y untouched:

df = pd.DataFrame(
    {"x1": [1.0, 2.0, 3.0], "x2": [4.0, 5.0, 6.0], "y": [7.0, 8.0, 9.0]}
)
recipe = Recipe().add(StepNormalize(s.starts_with("x")))
fitted = recipe.fit(df)
fitted.transform(df)
x1 x2 y
0 -1.0 -1.0 7.0
1 0.0 0.0 8.0
2 1.0 1.0 9.0

Methods

Name Description
boolean() All boolean columns.
boolean_features() All feature columns that are boolean-typed.
categorical() All categorical/enum columns (ordered or unordered).
categorical_features() All feature columns that are categorical.
col() The named column(s), if present.
contains() Columns whose name contains substring (case-sensitive).
date() All Date columns.
date_features() All feature columns that are Date-typed.
datetime() All Datetime columns.
datetime_features() All feature columns that are Datetime-typed.
double_features() All feature columns that are floating-point typed.
ends_with() Columns whose name ends with suffix.
everything() All columns.
features() All feature columns regardless of type.
float() All floating-point columns.
has_ancestor() Columns with the given step anywhere in their ancestry.
has_role() All columns carrying role.
has_source() Columns whose immediate producer is the given step.
integer() All integer columns (signed or unsigned).
integer_features() All feature columns that are integer-typed.
matches() Columns whose name matches the regular expression pattern.
nominal() All nominal columns (string, categorical, or enum).
nominal_features() All feature columns that are nominal (categorical, enum, or string).
num_range() Columns named f"{prefix}{i}" for i in rng (e.g. x1..x5).
numeric() All numeric columns: integer, floating-point, and decimal.
numeric_features() All feature columns that are numeric.
ordered() All ordered-categorical columns.
starts_with() Columns whose name starts with prefix.
string() All string columns.
string_features() All feature columns that are string-typed.
targets() All target columns regardless of type.
unordered() All unordered-categorical columns.
wrap() Lift a raw name / list / polars / narwhals selector into mlfeatures-land.

boolean()

All boolean columns.

Usage

Source

boolean()

boolean_features()

All feature columns that are boolean-typed.

Usage

Source

boolean_features()

categorical()

All categorical/enum columns (ordered or unordered).

Usage

Source

categorical()

categorical_features()

All feature columns that are categorical.

Usage

Source

categorical_features()

col()

The named column(s), if present.

Usage

Source

col(*names)

Names that aren’t in the data frame are silently skipped (tolerant, like R’s any_of). This differs from passing a plain list of names as a selector operand, which is strict and raises on a missing column (like all_of); use col when absence should be a no-op. A bare list that names a missing column raises the backend’s own column-not-found error, not a mlfeatures one.

Examples

"missing" is not a column, so it is skipped and only x1 is normalized:

recipe = Recipe().add(StepNormalize(s.col("x1", "missing")))
fitted = recipe.fit(df)
fitted.transform(df)
x1 x2 y
0 -1.0 4.0 7.0
1 0.0 5.0 8.0
2 1.0 6.0 9.0

contains()

Columns whose name contains substring (case-sensitive).

Usage

Source

contains(substring)

date()

All Date columns.

Usage

Source

date()

date_features()

All feature columns that are Date-typed.

Usage

Source

date_features()

datetime()

All Datetime columns.

Usage

Source

datetime()

datetime_features()

All feature columns that are Datetime-typed.

Usage

Source

datetime_features()

double_features()

All feature columns that are floating-point typed.

Usage

Source

double_features()

ends_with()

Columns whose name ends with suffix.

Usage

Source

ends_with(suffix)

Matching is case-sensitive and takes a single suffix, like starts_with.


everything()

All columns.

Usage

Source

everything()

features()

All feature columns regardless of type.

Usage

Source

features()

float()

All floating-point columns.

Usage

Source

float()

has_ancestor()

Columns with the given step anywhere in their ancestry.

Usage

Source

has_ancestor(step=None, *, step_type=None)

The ancestral counterpart of has_source: matches columns even after they were further transformed (e.g. dummies that were then normalized). Takes the same arguments as has_source, including its ValueError for an id or type that names no earlier step.


has_role()

All columns carrying role.

Usage

Source

has_role(role)

Roles are those of the recipe at fit time, so a column that an earlier step dropped is not selected. A role that no column carries selects nothing.


has_source()

Columns whose immediate producer is the given step.

Usage

Source

has_source(step=None, *, step_type=None)

Pass either a step id or step_type= (a Step subclass). Resolves against the lineage DAG at fit time. For columns that trace back to a step anywhere in their ancestry, use has_ancestor.

A step’s default id is its type’s short name, and a second step of the same type in one recipe is given a suffixed id ("dummy_2"), so an id here names exactly one step.

Unlike a dtype or role selector, matching nothing is an error, not the identity transform: raises ValueError if no step earlier in the recipe has that id (or is an instance of that type). Pass "original" as the id for the input columns that survive to this point. Passing both step and step_type, or neither, also raises ValueError.

Examples

Normalize only the indicator columns that StepDummy produced:

recipe = (
    Recipe()
    .add(StepDummy(s.string()))
    .add(StepNormalize(s.has_source(step_type=StepDummy)))
)
fitted = recipe.fit(df)
fitted.transform(df)
x city_b
0 1.0 -0.577350
1 2.0 1.154701
2 3.0 -0.577350

integer()

All integer columns (signed or unsigned).

Usage

Source

integer()

integer_features()

All feature columns that are integer-typed.

Usage

Source

integer_features()

matches()

Columns whose name matches the regular expression pattern.

Usage

Source

matches(pattern)

Uses re.search and is case-sensitive (anchor explicitly with ^/$ if desired, and use (?i) to ignore case).


nominal()

All nominal columns (string, categorical, or enum).

Usage

Source

nominal()

nominal_features()

All feature columns that are nominal (categorical, enum, or string).

Usage

Source

nominal_features()

num_range()

Columns named f"{prefix}{i}" for i in rng (e.g. x1..x5).

Usage

Source

num_range(prefix, rng)

Names that aren’t in the data frame are skipped, as with col. There is no zero-padding option (R’s width); spell padded names out with col.

Examples
recipe = Recipe().add(StepNormalize(s.num_range("x", range(1, 3))))
fitted = recipe.fit(df)
fitted.transform(df)
x1 x2 x3
0 -1.0 -1.0 7.0
1 0.0 0.0 8.0
2 1.0 1.0 9.0

numeric()

All numeric columns: integer, floating-point, and decimal.

Usage

Source

numeric()

Decimal columns only exist as such on polars; pandas stores them as object, which is not numeric.

This matches by dtype alone, so it also picks up the target and any ID column. In a step’s columns, prefer numeric_features, which also requires the feature role.


numeric_features()

All feature columns that are numeric.

Usage

Source

numeric_features()

Unlike numeric, this requires the feature role, so a numeric target or case-weights column is left out. It is the one to use in a step’s columns unless you really mean every numeric column. The other *_features selectors differ from their bare counterparts in the same way.


ordered()

All ordered-categorical columns.

Usage

Source

ordered()

narwhals reports a polars Enum and a pandas Categorical with ordered=True both as Enum, which is what this selects.


starts_with()

Columns whose name starts with prefix.

Usage

Source

starts_with(prefix)

Matching is case-sensitive and takes a single prefix. This differs from tidyselect, which ignores case by default and accepts several prefixes; combine selectors with | for the latter.


string()

All string columns.

Usage

Source

string()

string_features()

All feature columns that are string-typed.

Usage

Source

string_features()

targets()

All target columns regardless of type.

Usage

Source

targets()

unordered()

All unordered-categorical columns.

Usage

Source

unordered()

narwhals reports a polars Categorical and a pandas Categorical with ordered=False both as Categorical, which is what this selects.


wrap()

Lift a raw name / list / polars / narwhals selector into mlfeatures-land.

Usage

Source

wrap(selector)

Use this to let a polars or narwhals selector lead a composed expression, since cs.numeric() & mlfeatures_sel cannot work directly.

Examples
selector = s.wrap(cs.starts_with("x_")) & s.features()
recipe = Recipe().add(StepNormalize(selector))
fitted = recipe.fit(df)
fitted.transform(df)
x_1 x_2 y
0 -1.0 -1.0 7.0
1 0.0 0.0 8.0
2 1.0 1.0 9.0