Skip to main content

Four stages, eight steps

This page discusses the “Order the steps” box in the pre-processing workflow diagram below. Essentially, there are 8 positioning steps you can take to order your sequence of Step calls. Each position has its own page (e.g. “Missing: impute”).

fit(train)

surprise?
add a guard

2 · Order the steps

handle missing 
values (1–2)

wrangle
(3–5)

model prep
(6–7)

multivariate 
transforms (8) 

1 · Assign
roles

3 · Ensure robust
to new data
new data arrives

4 · Pass to the
scikit-learn pipeline

Overview of eight steps

Stage Position mlfeatures steps
1. Handle missing values 1. Missing: impute StepImputeMean, StepImputeMedian, StepImputeMode, StepIndicateNa
2. Missing: category levels StepUnknown, StepOther
2. Wrangle 3. Wrangle: transforms StepLog, StepOrderNorm, StepDateFeatures, StepDatetimeFeatures
4. Wrangle: discretize none; rarely advisable
5. Wrangle: dummy variables StepDummy, StepOneHot, StepHoliday
3. Model prep 6. Model prep: interactions StepInteract, StepPolynomial, StepSplineNatural, StepSplineMonotone
7. Model prep: normalize StepNormalize, StepRange
4. Multivariate transforms 8. Multivariate transforms StepPca, StepPls, StepClassDistance

Each stage exists so the next one works: handle missing first (impute the numeric gaps and give missing categorical levels an explicit value), because every later step wants complete columns, and the guard levels must exist before the dummy step freezes its vocabulary; wrangle everything into numbers next; model prep after, so interactions can cross indicator columns with numerics and normalize catches the freshly created columns; multivariate transforms last, because they want the finished, scaled matrix.

Example: eight steps in action

One recipe on the credit data can fill most of the positions. The first stages handle missing values and turn everything into numbers, and their order matters because each step decides what the next one sees: impute first so nothing downstream meets a null, then give missing and rare levels a name before the dummy step, so its vocabulary already contains the pooled level and new data cannot introduce a level it has no column for. StepUnknown names the missing values "unknown"; here they are rare enough that StepOther then pools them into "infrequent" with the other small levels.

from sklearn.model_selection import train_test_split

from mlfeatures import (
    Recipe,
    StepDropZeroVariance,
    StepDummy,
    StepImputeMedian,
    StepInteract,
    StepNormalize,
    StepOther,
    StepPca,
    StepUnknown,
)
from mlfeatures import Selectors as s
from mlfeatures.data import credit

credit_train, credit_validation = train_test_split(
    credit, train_size=0.75, stratify=credit["Status"], random_state=1
)

wrangled = (
    Recipe(data=credit_train, target="Status")
    .add(StepImputeMedian(s.numeric_features()))  # 1. impute
    .add(StepUnknown(s.nominal_features()))  # 2. category levels
    .add(StepOther(s.nominal_features(), threshold=0.05, other_level="infrequent"))
    .add(StepDummy(s.nominal_features()))  # 5. dummy variables
)
wrangled.fit(credit_train)
Recipe (trained)
  Inputs: 21 columns (20 features, 1 target)
  Steps:
    step_impute_median(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price)                                                                                                         [trained]
    step_unknown(Home, Marital, Records, Job)                                                                                                                                                       [trained]
    step_other(Home, Marital, Records, Job)                                                                                                                                                         [trained]
    step_dummy(Home, Marital, Records, Job → Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_single, Records_yes, Job_freelance, Job_infrequent, Job_partime)  [trained]

The dummy step’s resolved columns include Job_infrequent, the pooled level; Home’s few missing and rare values were pooled the same way, into the level its indicators are measured against. Model prep and the multivariate transform continue from there. Normalize sits after the interaction step so the new product column is scaled with the rest, and before PCA, which weights columns by their variance and would otherwise be dominated by the largest-valued ones.

recipe = (
    wrangled.add(StepDropZeroVariance(s.features()))  # filter
    .add(StepInteract(s.col("Income", "Amount"), keep_original_cols=True))  # 6. interactions
    .add(StepNormalize(s.numeric_features()))  # 7. normalize
    .add(StepPca(s.numeric_features(), num_comp=3))  # 8. multivariate
)
fitted = recipe.fit(credit_train)
fitted
Recipe (trained)
  Inputs: 4 columns (3 features, 1 target)
  Steps:
    step_impute_median(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price)                                                                                                                                                                               [trained]
    step_unknown(Home, Marital, Records, Job)                                                                                                                                                                                                                             [trained]
    step_other(Home, Marital, Records, Job)                                                                                                                                                                                                                               [trained]
    step_dummy(Home, Marital, Records, Job → Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_single, Records_yes, Job_freelance, Job_infrequent, Job_partime)                                                                        [trained]
    step_drop_zero_variance(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price, Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_single, Records_yes, Job_freelance, Job_infrequent, Job_partime)                    [trained]
    step_interact(Income, Amount → Income_x_Amount)                                                                                                                                                                                                                       [trained]
    step_normalize(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price, Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_single, Records_yes, Job_freelance, Job_infrequent, Job_partime, Income_x_Amount)            [trained]
    step_pca(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price, Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_single, Records_yes, Job_freelance, Job_infrequent, Job_partime, Income_x_Amount → PC1, PC2, PC3)  [trained]
fitted.transform(credit_validation).head()
shape: (5, 4)
StatusPC1PC2PC3
strf64f64f64
"good"3.117702-3.867043-2.708093
"bad"1.141765-0.733920.304952
"good"-3.426872-1.3217940.494303
"good"1.980484-0.5157852.314203
"good"-4.335174-0.985040.661233

Alternative perspective: feature type

While the eight steps focus on the order that steps tend to be put in, the crosswalk cross-references them with the kind of column each step works on (e.g. datetime features). This is often useful when you already know what step you want, or want to browse steps that return similar types of results.