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 |
Namespace of factory methods for built-in mlfeatures column selectors.
Usage
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.
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:
| 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. |
All boolean columns.
Usage
All feature columns that are boolean-typed.
Usage
All categorical/enum columns (ordered or unordered).
Usage
All feature columns that are categorical.
Usage
The named column(s), if present.
Usage
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.
"missing" is not a column, so it is skipped and only x1 is normalized:
Columns whose name contains substring (case-sensitive).
Usage
All Date columns.
Usage
All feature columns that are Date-typed.
Usage
All Datetime columns.
Usage
All feature columns that are Datetime-typed.
Usage
All feature columns that are floating-point typed.
Usage
Columns whose name ends with suffix.
Usage
Matching is case-sensitive and takes a single suffix, like starts_with.
All columns.
Usage
All feature columns regardless of type.
Usage
All floating-point columns.
Usage
Columns with the given step anywhere in their ancestry.
Usage
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.
All columns carrying role.
Usage
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.
Columns whose immediate producer is the given step.
Usage
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.
Normalize only the indicator columns that StepDummy produced:
All integer columns (signed or unsigned).
Usage
All feature columns that are integer-typed.
Usage
Columns whose name matches the regular expression pattern.
Usage
Uses re.search and is case-sensitive (anchor explicitly with ^/$ if desired, and use (?i) to ignore case).
All nominal columns (string, categorical, or enum).
Usage
All feature columns that are nominal (categorical, enum, or string).
Usage
Columns named f"{prefix}{i}" for i in rng (e.g. x1..x5).
Usage
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.
All numeric columns: integer, floating-point, and decimal.
Usage
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.
All feature columns that are numeric.
Usage
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.
All ordered-categorical columns.
Usage
narwhals reports a polars Enum and a pandas Categorical with ordered=True both as Enum, which is what this selects.
Columns whose name starts with prefix.
Usage
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.
All string columns.
Usage
All feature columns that are string-typed.
Usage
All target columns regardless of type.
Usage
All unordered-categorical columns.
Usage
narwhals reports a polars Categorical and a pandas Categorical with ordered=False both as Categorical, which is what this selects.
Lift a raw name / list / polars / narwhals selector into mlfeatures-land.
Usage
Use this to let a polars or narwhals selector lead a composed expression, since cs.numeric() & mlfeatures_sel cannot work directly.