Skip to main content

With polars extension (.mlf)

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)
priceqtycity_NYCcity_SF
f64i64i8i8
10.0210
20.0100
null510
30.0101
25.0300
12.0401

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.

Handle missing values: step_impute_median

step_impute_median fills nulls with the column’s median (step_impute_mean and step_impute_mode are the other flavors):

filled = orders.mlf.step_impute_median(cs.by_name("price"))
filled
shape: (6, 3)
citypriceqty
strf64i64
"NYC"10.02
"LA"20.01
"NYC"20.05
"SF"30.01
"LA"25.03
"SF"12.04

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)
cityPC1PC2
strf64f64
"NYC"-9.431613-1.318761
"LA"0.613495-1.628313
"NYC"0.3382632.362207
"SF"10.589794-0.940235
"LA"5.4640290.710986
"SF"-7.5739680.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)
citypriceqty
strf64f64
"NYC"-1.252823-0.408248
"LA"0.065938-1.020621
"NYC"0.0659381.428869
"SF"1.384699-1.020621
"LA"0.7253190.204124
"SF"-0.9890710.816497