04.03 Lesson 3 of 16
1.2 hFeature 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
ExerciseBuild a mixed-type pipeline
Prepare a DataFrame with numeric and categorical columns, then fit a classifier through one complete pipeline.
- List numeric and categorical feature names explicitly.
- Build a numeric pipeline with median imputation and standardization.
- Build a categorical pipeline with most-frequent imputation and one-hot encoding.
- Combine both branches with ColumnTransformer and add a classifier with Pipeline.
- Fit only on training rows and predict a validation partition containing an unseen category.
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
- 01 Preprocessing data Article
Read standardization, imputation and one-hot encoding. Focus on the assumptions of each transformer.
- 02 Pipelines and composite estimators Article
Read Pipeline and ColumnTransformer, then reproduce the mixed-type transformation example.
Check your understanding
Self-test
-
Question 1
Why use a ColumnTransformer?
It applies different preprocessing steps to different column groups, such as scaling numeric features and one-hot encoding categories. -
Question 2
Why place preprocessing inside a Pipeline?
Cross-validation and model fitting then learn transformations only from each training fold and reuse them consistently for held-out rows.
Completion check
Move on when
You can fit and evaluate mixed-type data with one pipeline, explain each transformer and handle unseen categories safely.