mlfeatures ships an opt-in polars accessor for fitting and applying steps in one expression, without writing out a Recipe. Importing mlfeatures.polars registers the mlf accessor on polars DataFrames. The accessor is polars-only: for pandas data frames, use a Recipe.
import polars as pl
import polars.selectors as cs
import mlfeatures.polars # registers the `df.mlf` accessor
orders = pl.DataFrame(
{
"city": ["NYC", "LA", "NYC", "SF", "LA", "SF"],
"price": [10.0, 20.0, None, 30.0, 25.0, 12.0],
"qty": [2, 1, 5, 1, 3, 4],
}
)
orders.mlf.step_dummy(cs.string())
shape: (6, 4)| price | qty | city_NYC | city_SF |
|---|
| f64 | i64 | i8 | i8 |
| 10.0 | 2 | 1 | 0 |
| 20.0 | 1 | 0 | 0 |
| null | 5 | 1 | 0 |
| 30.0 | 1 | 0 | 1 |
| 25.0 | 3 | 0 | 0 |
| 12.0 | 4 | 0 | 1 |
Notice that .step_dummy() returned a new DataFrame with a column for each level of city except one, which is the reference level that all-zero indicators stand for. Use step_one_hot to get a column for every level.
In the following sections we’ll show some of the more useful steps to use directly on a polars DataFrame.
Fit and apply happen together
Every df.mlf.step_*() call builds the step, fits it on the data frame, transforms that same data frame, and returns the result. This makes the accessor a quick way to do a transformation once, whether you are trying a step out or cleaning up a data frame you will not transform again.
The fitted step is discarded, so nothing it estimated (a median, the levels of a column, PCA loadings) can be reused on other data. Calling the accessor again on a test set re-estimates everything from the test set, which leaks information and gives you different values than the training data got. When the same transform has to be applied to new data, build a Recipe and fit it once.
Multivariate feature engineering: step_pca
step_pca replaces numeric columns with their projection onto principal components (here on the imputed data frame, since PCA needs complete data):
(
orders
.mlf.step_impute_median(cs.by_name("price"))
.mlf.step_pca(cs.numeric(), num_comp=2)
)
shape: (6, 3)| city | PC1 | PC2 |
|---|
| str | f64 | f64 |
| "NYC" | -9.431613 | -1.318761 |
| "LA" | 0.613495 | -1.628313 |
| "NYC" | 0.338263 | 2.362207 |
| "SF" | 10.589794 | -0.940235 |
| "LA" | 5.464029 | 0.710986 |
| "SF" | -7.573968 | 0.814115 |
Chaining and other steps
The accessor is available on every result, so calls chain, as above. df.mlf.apply() takes any step objects, which reaches steps that have no step_* wrapper and custom steps. Each step is fitted in order on the data frame the previous one produced:
from mlfeatures import Selectors as s
from mlfeatures import StepImputeMedian, StepNormalize
orders.mlf.apply(
StepImputeMedian(s.numeric_features()),
StepNormalize(s.numeric_features()),
)
shape: (6, 3)| city | price | qty |
|---|
| str | f64 | f64 |
| "NYC" | -1.252823 | -0.408248 |
| "LA" | 0.065938 | -1.020621 |
| "NYC" | 0.065938 | 1.428869 |
| "SF" | 1.384699 | -1.020621 |
| "LA" | 0.725319 | 0.204124 |
| "SF" | -0.989071 | 0.816497 |