04.03 Lesson 3 of 16

1.2 h

Feature Preparation & Pipelines

Prepare mixed numeric and categorical columns with imputers, scaling and encoding inside a single scikit-learn pipeline.

By the end of this lesson

You will be able to

  • Explain imputation, standardization and one-hot encoding.
  • Choose preprocessing based on feature type and model assumptions.
  • Build a ColumnTransformer for mixed data.
  • Explain how a pipeline prevents fold-level leakage.

Different columns need different treatment

Numeric features may need missing-value imputation and scaling. Categorical features usually need missing-value handling and encoding into model-compatible columns. StandardScaler centres and scales numeric values; OneHotEncoder creates indicator columns for categories. Scaling matters especially for distance-based and margin-based methods, while tree splits are generally less sensitive to scale. Do not encode nominal categories as integers unless their numeric order is meaningful.

from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

numeric = Pipeline([
    ("impute", SimpleImputer(strategy="median")),
    ("scale", StandardScaler()),
])
categorical = Pipeline([
    ("impute", SimpleImputer(strategy="most_frequent")),
    ("encode", OneHotEncoder(handle_unknown="ignore")),
])

A pipeline makes preprocessing part of the fitted model

A ColumnTransformer applies different transformations to selected column groups. Place it inside a Pipeline before the estimator. When the pipeline is fitted on a training fold, each imputer, scaler and encoder learns only from that fold. At prediction time, the same stored transformations are applied to new rows. Keep DataFrame column names until the transformer selects each group, and explicitly handle categories that were not present during training.

preprocess = ColumnTransformer([
    ("numeric", numeric, numeric_columns),
    ("categorical", categorical, categorical_columns),
])
model = Pipeline([
    ("prepare", preprocess),
    ("classifier", LogisticRegression(max_iter=1000)),
])

Apply what you learned

Exercise

Build a mixed-type pipeline

Prepare a DataFrame with numeric and categorical columns, then fit a classifier through one complete pipeline.

  1. List numeric and categorical feature names explicitly.
  2. Build a numeric pipeline with median imputation and standardization.
  3. Build a categorical pipeline with most-frequent imputation and one-hot encoding.
  4. Combine both branches with ColumnTransformer and add a classifier with Pipeline.
  5. Fit only on training rows and predict a validation partition containing an unseen category.
Done when

The pipeline fits without preprocessing validation rows, handles an unseen category and can be passed directly to cross-validation or search.

Free references

Study resources

2 links
  1. 01
    Preprocessing data Article

    Read standardization, imputation and one-hot encoding. Focus on the assumptions of each transformer.

  2. 02
    Pipelines and composite estimators Article

    Read Pipeline and ColumnTransformer, then reproduce the mixed-type transformation example.

Check your understanding

Self-test

2 questions
  1. Question 1

    Why use a ColumnTransformer?

  2. Question 2

    Why place preprocessing inside a Pipeline?

Completion check

Move on when

You can fit and evaluate mixed-type data with one pipeline, explain each transformer and handle unseen categories safely.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels