from pathlib import Path
import numpy as np
import pandas as pd
import os
from sklearn.compose import ColumnTransformer, make_column_transformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_validate, train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder, StandardScaler
DATA_DIR = Path("data")Chapter 4: Preprocessing, Pipelines, and Column Transformers
By the end of this chapter, you will be able to:
- Explain why many machine learning models require features to be preprocessed.
- Distinguish a transformer from a predictive estimator in the
scikit-learnAPI. - Use
SimpleImputer,StandardScaler,OrdinalEncoder, andOneHotEncoderappropriately. - Explain why a transformer must be fit using only the training data available at that stage of the workflow.
- Identify data leakage caused by preprocessing before a train/test split or outside cross-validation.
- Build and evaluate a preprocessing pipeline using
make_pipeline. - Choose transformations for different feature types and combine them with
ColumnTransformer. - Evaluate a complete mixed-feature pipeline with cross-validation.
Chapter 3 introduced models that make predictions using distances between examples. Those models exposed an important practical problem: a distance calculation treats the numbers in a feature matrix literally, even when the numbers use very different units. Real datasets introduce further complications, including missing values and categorical features that are not numbers at all.
Preprocessing transforms the original features into a representation that a machine learning model can use. Choosing the transformations is part of model development: a useful representation can help a model find meaningful patterns, while an inappropriate representation can hide them. Just as importantly, preprocessing must respect the Golden Rule from Chapter 2. Any information learned from data during preprocessing must come only from the training data available at that point in the workflow.
The accompanying videos introduce the motivation for preprocessing, imputation and scaling, and categorical features.
Imports
Why preprocessing matters
Suppose we want to match a student with a compatible roommate. We use two features, the number of quiet hours each student prefers per week and their number of followers on social media, and measure similarity using Euclidean distance. Our student prefers 40 quiet hours per week and has 25,000 followers.
roommate_candidates = pd.DataFrame(
{
"quiet_hours_per_week": [35, 37, 40, 10],
"followers": [40000, 30000, 50000, 25000],
},
index=["A", "B", "C", "D"],
)
student = pd.Series({"quiet_hours_per_week": 40, "followers": 25000})
roommate_candidates.assign(
distance=np.sqrt(((roommate_candidates - student) ** 2).sum(axis=1))
).sort_values(
"distance"
)| quiet_hours_per_week | followers | distance | |
|---|---|---|---|
| D | 10 | 25000 | 30.000000 |
| B | 37 | 30000 | 5000.000900 |
| A | 35 | 40000 | 15000.000833 |
| C | 40 | 50000 | 25000.000000 |
The calculation identifies D as the nearest neighbour, with a distance of 30. This is surprising: D prefers 30 fewer quiet hours per week than our student, whereas B differs by only 3 quiet hours and has 5,000 more followers. Nevertheless, B’s distance is about 5,000 because the follower-count difference overwhelms the quiet-hour difference. C prefers exactly the same 40 quiet hours as our student, yet is ranked as the farthest away, with a distance of 25,000, entirely because C has 25,000 more followers. For roommate compatibility, we might reasonably care much more about similar living preferences than similar follower counts.
The calculation treats a difference of one follower as numerically equivalent to a difference of one quiet hour. It is mathematically valid, but the representation is the problem: hours and followers use unrelated units, and there is no reason that one unit of each should count equally. Measuring followers in thousands instead would make B the nearest neighbour, even though nothing about the students had changed. A model whose behaviour changes merely because we changed units is often undesirable.
Scaling with StandardScaler
One common solution is standardization. For each feature, StandardScaler subtracts the mean and divides by the standard deviation:
\[z = \frac{x - \mu}{\sigma}.\]
After this transformation, each feature in the training data has mean 0 and standard deviation 1. Values are now measured in standard deviations from that feature’s training mean. Standardization does not force values into a fixed minimum and maximum, and it does not make unlike quantities identical. It simply puts their numerical variation on a comparable scale.
scaler = StandardScaler()
candidates_scaled = scaler.fit_transform(roommate_candidates)
student_scaled = scaler.transform(student.to_frame().T)
pd.DataFrame(
candidates_scaled,
columns=roommate_candidates.columns,
index=roommate_candidates.index,
).assign(
distance=np.sqrt(((candidates_scaled - student_scaled) ** 2).sum(axis=1))
).sort_values("distance")| quiet_hours_per_week | followers | distance | |
|---|---|---|---|
| B | 0.543083 | -0.650945 | 0.577939 |
| A | 0.375980 | 0.390567 | 1.617157 |
| D | -1.712800 | -1.171700 | 2.506536 |
| C | 0.793736 | 1.432078 | 2.603778 |
The nearest neighbours change after scaling because quiet-hour preferences can now contribute meaningfully to the distance. This is why scaling is usually important for \(k\)-NN and RBF SVM models. It is also important for many models introduced later in the book. Decision trees are generally insensitive to scaling: replacing a split such as quiet_hours_per_week <= 35 with its equivalent on a standardized feature does not change which examples fall on either side.
Scaling is not automatically beneficial for every dataset or model. It encodes a modelling choice that the observed variation of each feature should initially have comparable influence. Domain knowledge may suggest a different representation.
A running example: predicting quiz 2 grades
For the rest of this chapter, we will use a small dataset about students in a fictional course. The prediction task is to predict whether a student will receive an A+ on quiz 2. The features include previous grades, university experience, major, class attendance, and whether the student enjoys the course.
quiz_data = pd.read_csv(DATA_DIR / "quiz2-grade-toy-col-transformer.csv")
quiz_data| enjoy_course | ml_experience | major | class_attendance | university_years | lab1 | lab2 | lab3 | lab4 | quiz1 | quiz2 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | yes | 1 | Computer Science | Excellent | 3 | 92 | 93.0 | 84 | 91 | 92 | A+ |
| 1 | yes | 1 | Mechanical Engineering | Average | 2 | 94 | 90.0 | 80 | 83 | 91 | not A+ |
| 2 | yes | 0 | Mathematics | Poor | 3 | 78 | 85.0 | 83 | 80 | 80 | not A+ |
| 3 | no | 0 | Mathematics | Excellent | 3 | 91 | NaN | 92 | 91 | 89 | A+ |
| 4 | yes | 0 | Psychology | Good | 4 | 77 | 83.0 | 90 | 92 | 85 | A+ |
| 5 | no | 1 | Economics | Good | 5 | 70 | 73.0 | 68 | 74 | 71 | not A+ |
| 6 | yes | 1 | Computer Science | Excellent | 4 | 80 | 88.0 | 89 | 88 | 91 | A+ |
| 7 | no | 0 | Mechanical Engineering | Poor | 3 | 95 | 93.0 | 69 | 79 | 75 | not A+ |
| 8 | no | 0 | Linguistics | Average | 2 | 97 | 90.0 | 94 | 82 | 80 | not A+ |
| 9 | yes | 1 | Mathematics | Average | 4 | 95 | 82.0 | 94 | 94 | 85 | not A+ |
| 10 | yes | 0 | Psychology | Good | 3 | 98 | 86.0 | 95 | 95 | 78 | A+ |
| 11 | yes | 1 | Physics | Average | 1 | 95 | 88.0 | 93 | 92 | 85 | A+ |
| 12 | yes | 1 | Physics | Excellent | 2 | 98 | 96.0 | 96 | 99 | 100 | A+ |
| 13 | yes | 0 | Mechanical Engineering | Excellent | 4 | 95 | 94.0 | 96 | 95 | 100 | A+ |
| 14 | no | 0 | Mathematics | Poor | 3 | 95 | 90.0 | 93 | 95 | 70 | not A+ |
| 15 | no | 1 | Computer Science | Good | 3 | 92 | 85.0 | 67 | 94 | 92 | not A+ |
| 16 | yes | 0 | Computer Science | Average | 5 | 75 | 91.0 | 93 | 86 | 85 | A+ |
| 17 | yes | 1 | Economics | Average | 3 | 86 | 89.0 | 65 | 86 | 87 | not A+ |
| 18 | no | 1 | Biology | Good | 2 | 91 | NaN | 90 | 88 | 82 | not A+ |
| 19 | no | 0 | Psychology | Poor | 2 | 77 | 94.0 | 87 | 81 | 89 | not A+ |
| 20 | yes | 1 | Linguistics | Excellent | 4 | 96 | 92.0 | 92 | 96 | 87 | A+ |
This dataset contains only 21 invented examples. It is deliberately small so that we can inspect every transformation, and its patterns were constructed for teaching. Validation and test scores will be unstable and must not be interpreted as evidence that these features predict real students’ performance. In particular, variables such as major should not be treated as explanations of ability or used to make decisions about students. Our goal is to learn the mechanics of preprocessing, not to build a deployable student-performance model.
We split the data before inspecting or fitting transformations. This gives us one training set that we can carry through the rest of the chapter.
X = quiz_data.drop(columns="quiz2")
y = quiz_data["quiz2"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=123, stratify=y
)
print('Number of training examples: ', X_train.shape)
print('Number of test examples: ', X_test.shape)Number of training examples: (16, 10)
Number of test examples: (5, 10)
Common transformers in scikit-learn
We will now use the running example to examine how transformers learn and apply a representation. We begin with their shared API, then apply scaling, imputation, and categorical encoding.
The transformer API: fit, transform, and fit_transform
Chapter 1 introduced predictive estimators such as classifiers. A classifier learns a relationship between features and targets using fit(X, y), then produces target predictions using predict(X).
A transformer instead learns how to change the feature representation. It uses two main methods:
fit(X)learns quantities needed by the transformation. ForStandardScaler, these are the mean and standard deviation of each feature.transform(X)applies the learned transformation and returns a new representation.
Calling fit_transform(X) is shorthand for fitting and then transforming the same data. It is convenient for training data. For validation, test, or deployment data, we call only transform, using the transformer already fit on the training data.
Scaling numeric features with StandardScaler
Let’s apply the same scaling idea to the numeric grade features in our running example. We’ll first use the scaler directly to understand what it learns, then combine it with other preprocessing steps in a pipeline.
For now, we select grade columns without missing values so that we can focus on scaling. The next subsection addresses the missing entries in lab2.
numeric_features = ["university_years", "lab1", "lab2", "lab3", "lab4", "quiz1"]
scaler = StandardScaler()
grade_features = ["lab1", "lab3", "lab4", "quiz1"]
X_train_scaled = scaler.fit_transform(X_train[grade_features])
X_test_scaled = scaler.transform(X_test[grade_features])
pd.DataFrame(X_train_scaled, columns=grade_features, index=X_train.index).head()| lab1 | lab3 | lab4 | quiz1 | |
|---|---|---|---|---|
| 6 | -1.272132 | 0.254880 | -0.203214 | 0.676206 |
| 5 | -2.505715 | -1.606852 | -2.370832 | -1.625773 |
| 8 | 0.824958 | 0.698149 | -1.132193 | -0.589882 |
| 7 | 0.578242 | -1.518198 | -1.596683 | -1.165377 |
| 3 | 0.084809 | 0.520842 | 0.261275 | 0.446009 |
The scaler learns one mean and standard deviation for each grade feature from X_train. We use those same training statistics to transform X_test; we do not refit the scaler on the test set. Refitting would use information from the test distribution and would give each feature a different meaning in the two datasets.
Some transformers accept y in fit, and a few genuinely use the target. The important question is not whether a step looks like conventional model fitting. It is whether the step learns anything from the data. If it does, it must obey the same data-separation rules as the predictive model.
Missing values and imputation with SimpleImputer
Real datasets frequently contain missing values. A sensor may fail, a survey participant may skip a question, or two tables may not match perfectly when joined. Many estimators, including KNeighborsClassifier, cannot directly process NaN values.
One option is to remove rows or features containing missing values, but this can discard useful information and can introduce bias when values are not missing at random. Another option is imputation: replacing each missing value according to a specified rule.
X_train.loc[X_train["lab2"].isna(), numeric_features]| university_years | lab1 | lab2 | lab3 | lab4 | quiz1 | |
|---|---|---|---|---|---|---|
| 3 | 3 | 91 | NaN | 92 | 91 | 89 |
| 18 | 2 | 91 | NaN | 90 | 88 | 82 |
SimpleImputer(strategy="median") learns the median of each feature during fit and uses those values to replace missing entries during transform. The median can be preferable to the mean when unusually large or small observations are present, although neither strategy reconstructs the unknown value or removes the reason it was missing.
imputer = SimpleImputer(strategy="median").set_output(transform="pandas")
X_train_numeric_imputed = imputer.fit_transform(X_train[numeric_features])
X_test_numeric_imputed = imputer.transform(X_test[numeric_features])
X_train_numeric_imputed.loc[X_train["lab2"].isna()]| university_years | lab1 | lab2 | lab3 | lab4 | quiz1 | |
|---|---|---|---|---|---|---|
| 3 | 3.0 | 91.0 | 89.5 | 92.0 | 91.0 | 89.0 |
| 18 | 2.0 | 91.0 | 89.5 | 90.0 | 88.0 | 82.0 |
The learned replacement values are available in imputer.statistics_. As with scaling, they must be learned from the training data. Computing a median using the entire dataset before splitting would let the test examples influence the representation used to train the model.
Imputation makes the data usable, but it also makes an assumption. Median imputation treats a missing entry as typical for that feature and reduces its apparent variation. In an applied project, we should investigate why values are missing and consider whether the fact that a value is missing is itself informative.
Nominal categories and OneHotEncoder
A categorical feature records membership in a set of categories, such as a neighbourhood, product type, or spoken language. Most scikit-learn estimators require numeric feature matrices, so these categories need a numerical representation. The representation should reflect what the categories mean.
For a nominal feature, one-hot encoding creates one binary feature for each observed category. Exactly one of these new features is 1 for each original value, and the rest are 0. This avoids imposing an arbitrary order.
majors = X_train[["major"]]
one_hot_encoder = OneHotEncoder(sparse_output=False).set_output(transform="pandas")
one_hot_encoder.fit_transform(majors).head()| major_Biology | major_Computer Science | major_Economics | major_Linguistics | major_Mathematics | major_Mechanical Engineering | major_Physics | major_Psychology | |
|---|---|---|---|---|---|---|---|---|
| 6 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
| 5 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
| 8 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 |
| 7 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 |
| 3 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 |
The encoder learns the categories during fit, creating a fixed set of output columns. Chapter 5 examines what happens when a new category appears and how to handle features with many categories.
Ordered categories and OrdinalEncoder
OrdinalEncoder represents each category with a different number. This is appropriate when the categories have a meaningful order, such as poor, average, and good, and we explicitly supply that order.
For an unordered—or nominal—feature such as language, arbitrary integer codes are usually misleading. A distance-based model would interpret categories coded 0 and 1 as closer than categories coded 0 and 4. The numerical ordering came from the encoding, not the problem.
attendance = X_train[["class_attendance"]]
ordinal_encoder = OrdinalEncoder(
categories=[["Poor", "Average", "Good", "Excellent"]]
)
attendance.assign(
attendance_encoded=ordinal_encoder.fit_transform(attendance).ravel()
).head()| class_attendance | attendance_encoded | |
|---|---|---|
| 6 | Excellent | 3.0 |
| 5 | Good | 2.0 |
| 8 | Average | 1.0 |
| 7 | Poor | 0.0 |
| 3 | Excellent | 3.0 |
Even when categories are ordered, their spacing requires thought. Encoding Poor, Average, Good, and Excellent as 0, 1, 2, and 3 tells the model that the gaps are equal. Some models can use that simplification effectively, but it remains an assumption rather than a fact about the labels.
Combining preprocessing and prediction with a pipeline
A scikit-learn pipeline chains transformers followed by a final estimator. From the outside, the entire pipeline behaves like one predictive estimator: it supports fit, predict, and score. Internally, it applies the transformations in order before passing the resulting features to the model.
We have now seen the individual transformers. A pipeline brings them together with a model and keeps their fitting steps within the evaluation boundaries described below.
Why preprocessing must stay inside cross-validation
The Golden Rule says that the test data must not influence model development. For preprocessing, the practical version is:
Split first. Fit each transformer using only the training data, then use that fitted transformer to transform both the training and test data.
This rule also applies inside cross-validation. Each fold temporarily treats part of the training set as validation data. Therefore, the transformer must be fit separately on the training portion of each fold.
Consider this tempting workflow:
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
cross_validate(knn, X_train_scaled, y_train)Although the test set is untouched, the scaler is fit before the cross-validation folds are created. Consequently, the mean and standard deviation used in every fold include its validation examples. This is data leakage: information crosses a boundary that is supposed to simulate unseen data. The estimated validation performance may therefore be too optimistic.
The same issue occurs with imputation, category discovery, feature selection, and any other data-dependent transformation. Manually coordinating every transformation within every fold would be tedious and error-prone. A pipeline automates that coordination.
Build and evaluate the numeric pipeline
We will first build a pipeline using only the numeric features from the quiz-grade dataset. This restriction lets the same imputer and scaler operate on every input column. It is temporary: excluding the categorical and ordinal features sets up the problem we will solve later in this chapter.
X_train_numeric = X_train[numeric_features]
X_test_numeric = X_test[numeric_features]
X_train_numeric.head()| university_years | lab1 | lab2 | lab3 | lab4 | quiz1 | |
|---|---|---|---|---|---|---|
| 6 | 4 | 80 | 88.0 | 89 | 88 | 91 |
| 5 | 5 | 70 | 73.0 | 68 | 74 | 71 |
| 8 | 2 | 97 | 90.0 | 94 | 82 | 80 |
| 7 | 3 | 95 | 93.0 | 69 | 79 | 75 |
| 3 | 3 | 91 | NaN | 92 | 91 | 89 |
quiz_pipe = make_pipeline(
SimpleImputer(strategy="median"),
StandardScaler(),
KNeighborsClassifier(),
)
quiz_pipePipeline(steps=[('simpleimputer', SimpleImputer(strategy='median')),
('standardscaler', StandardScaler()),
('kneighborsclassifier', KNeighborsClassifier())])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
Parameters
Parameters
Parameters
When quiz_pipe.fit(X_train_numeric, y_train) is called, the pipeline performs these operations in sequence:
- Fit the imputer on
X_train_numeric, then transformX_train_numeric. - Fit the scaler on the imputed training data, then transform those data.
- Fit the \(k\)-NN classifier on the resulting feature matrix and
y_train.
When predict(X_test_numeric) is called, the already-fitted imputer and scaler transform X_test_numeric; neither transformer is refit. The fitted classifier then predicts from the transformed test features.
cv_results = cross_validate(
quiz_pipe, X_train_numeric, y_train, cv=5, return_train_score=True
)
pd.DataFrame(cv_results)[["train_score", "test_score"]].agg(["mean", "std"]).T| mean | std | |
|---|---|---|
| train_score | 0.719231 | 0.037488 |
| test_score | 0.633333 | 0.341565 |
Passing the pipeline to cross_validate is crucial. For each fold, scikit-learn fits a fresh copy of the entire pipeline on that fold’s training portion. The imputer and scaler therefore learn nothing from the validation portion. The fitted transformations are applied to the validation data before scoring.
After using cross-validation to make modelling decisions, we can fit the selected pipeline on all of X_train and evaluate it once on X_test.
quiz_pipe.fit(X_train_numeric, y_train)
quiz_pipe.score(X_test_numeric, y_test)0.6
The test score is not automatically trustworthy merely because we used a pipeline. We must still avoid repeatedly checking it while selecting features, transformations, models, or hyperparameters. A pipeline protects the boundaries within a specified evaluation procedure; it cannot prevent us from choosing the procedure based on test results. Moreover, this particular score is based on only a handful of invented test examples. It demonstrates the API, not the expected performance of a real student model.
Different transformations for different features
A plain pipeline applies each step to all the features it receives. Our numeric pipeline works for grades, but scaling the string major would fail. One-hot encoding every grade would also discard its numerical meaning. We need separate paths for different column groups.
ColumnTransformer sends selected columns to separate transformers, then joins their outputs horizontally into one feature matrix. A branch can itself be a pipeline: our numeric columns need imputation followed by scaling. The column transformer prepares features; the outer pipeline adds the predictive model.
Plan transformations by feature type
Before writing code, we should decide what each column represents and what the model needs from its representation.
| Features | Type | Planned transformation | Reasoning |
|---|---|---|---|
university_years, lab1–lab4, quiz1 |
Numeric | Median imputation, then standardization | lab2 contains missing values, which the estimator cannot accept. Scaling prevents a feature’s numerical spread from determining its influence on a distance- or kernel-based model. |
major |
Nominal categorical | One-hot encoding | The majors have no meaningful order, so integer codes would introduce arbitrary distances. |
class_attendance |
Ordinal categorical | Ordinal encoding with an explicit order | The labels Poor, Average, Good, and Excellent have a meaningful ranking that we want to preserve. |
enjoy_course |
Binary categorical | One-hot encoding | The values are categories rather than measured numbers. Two indicator columns provide a simple initial representation; one would also be sufficient. |
ml_experience |
Already numeric and binary | Pass through unchanged | The column is already represented as 0 and 1, so no encoding is required. |
These choices are plausible for this teaching example, not universal rules. For instance, ordinal encoding assumes that adjacent attendance levels have comparable spacing. The appropriate transformation always depends on what a feature means and how the downstream model uses it.
Build a ColumnTransformer
We keep the training/test split already created above. Column lists describe the original input features, not columns that have already been transformed. For this first workflow, we keep both indicator columns for enjoy_course; Chapter 5 discusses using a single indicator instead.
categorical_features = ["major", "enjoy_course"]
ordinal_features = ["class_attendance"]
passthrough_features = ["ml_experience"]
attendance_levels = ["Poor", "Average", "Good", "Excellent"]
numeric_pipe = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())
preprocessor = make_column_transformer(
(numeric_pipe, numeric_features),
(OneHotEncoder(handle_unknown="ignore", sparse_output=False), categorical_features),
(OrdinalEncoder(categories=[attendance_levels]), ordinal_features),
("passthrough", passthrough_features),
)
preprocessorColumnTransformer(transformers=[('pipeline',
Pipeline(steps=[('simpleimputer',
SimpleImputer(strategy='median')),
('standardscaler',
StandardScaler())]),
['university_years', 'lab1', 'lab2', 'lab3',
'lab4', 'quiz1']),
('onehotencoder',
OneHotEncoder(handle_unknown='ignore',
sparse_output=False),
['major', 'enjoy_course']),
('ordinalencoder',
OrdinalEncoder(categories=[['Poor', 'Average',
'Good',
'Excellent']]),
['class_attendance']),
('passthrough', 'passthrough',
['ml_experience'])])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
['university_years', 'lab1', 'lab2', 'lab3', 'lab4', 'quiz1']
Parameters
Parameters
['major', 'enjoy_course']
Parameters
['class_attendance']
Parameters
Each tuple gives a transformer and the columns it receives. "passthrough" keeps an already suitable representation unchanged. Unlisted columns are dropped by default; we can also explicitly assign columns to "drop". Always check that an omitted column was omitted intentionally.
We use handle_unknown="ignore" here so that a category absent from a cross-validation training fold will not cause an error when that fold is validated. It represents an unseen category with zeros in that feature’s indicator columns. Chapter 5 explores the consequences of this choice.
The explicit ColumnTransformer constructor allows custom branch names. For example, the numeric branch alone could be written as ColumnTransformer([("numeric", numeric_pipe, numeric_features)]). make_column_transformer generates names for us.
Inspect the transformed features
For inspection, fit the preprocessor on the training data and use get_feature_names_out to label its output. Output columns follow branch order, which may differ from their order in the original DataFrame. The dense output requested here is convenient for this tiny example.
X_train_prepared = preprocessor.fit_transform(X_train)
pd.DataFrame(
X_train_prepared,
columns=preprocessor.get_feature_names_out(),
index=X_train.index,
).head()| pipeline__university_years | pipeline__lab1 | pipeline__lab2 | pipeline__lab3 | pipeline__lab4 | pipeline__quiz1 | onehotencoder__major_Biology | onehotencoder__major_Computer Science | onehotencoder__major_Economics | onehotencoder__major_Linguistics | onehotencoder__major_Mathematics | onehotencoder__major_Mechanical Engineering | onehotencoder__major_Physics | onehotencoder__major_Psychology | onehotencoder__enjoy_course_no | onehotencoder__enjoy_course_yes | ordinalencoder__class_attendance | passthrough__ml_experience | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 6 | 0.830747 | -1.272132 | -0.107090 | 0.254880 | -0.203214 | 0.676206 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 3.0 | 1.0 |
| 5 | 1.780172 | -2.505715 | -2.962816 | -1.606852 | -2.370832 | -1.625773 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 2.0 | 1.0 |
| 8 | -1.068103 | 0.824958 | 0.273674 | 0.698149 | -1.132193 | -0.589882 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 |
| 7 | -0.118678 | 0.578242 | 0.844819 | -1.518198 | -1.596683 | -1.165377 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 |
| 3 | -0.118678 | 0.084809 | 0.178483 | 0.520842 | 0.261275 | 0.446009 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 3.0 | 0.0 |
The diagram shows how the separate branches join their outputs:

Evaluate the complete pipeline
Inspecting transformed training features is useful, but do not pass this already transformed matrix to cross-validation. Pass the raw training DataFrame and the complete pipeline. That lets each fold learn its own imputation values, scaling statistics, and categories.
mixed_pipe = make_pipeline(preprocessor, KNeighborsClassifier())
mixed_cv = cross_validate(
mixed_pipe, X_train, y_train, cv=5, return_train_score=True
)
pd.DataFrame(mixed_cv)[["train_score", "test_score"]].agg(["mean", "std"]).T| mean | std | |
|---|---|---|
| train_score | 0.811538 | 0.072467 |
| test_score | 0.816667 | 0.170783 |
Within each fold, the numeric branch fits the imputer and then the scaler; the categorical and ordinal branches fit on their assigned columns. Their outputs are joined, and the classifier is fit on that matrix. Validation uses those fitted transformations without refitting. test_score in the cross-validation result means the validation score for that fold, not a score on our held-out X_test.
After choosing a workflow using training-set cross-validation, fit it on all the training data. The following shows the final evaluation step; in an applied project, reserve this step until model selection is complete.
mixed_pipe.fit(X_train, y_train)
mixed_pipe.score(X_test, y_test)1.0
From Chapters 3 and 4 to Lab 2
You now have the components for a mixed-feature prediction workflow: split off a test set, identify feature types, choose transformations, put them in a column transformer, and place that preprocessor before a model. Compare a baseline with models from Chapter 3 using cross-validation on the training data. Fit the selected workflow on all the training data before evaluating the test set once.
When adapting this pattern, use your dataset’s column names and justify the choices from feature meaning. Missing categorical values may require a categorical pipeline with SimpleImputer(strategy="most_frequent") before the encoder. Check that every feature would be available at prediction time and that the target has not accidentally entered the feature lists.
Exercises
Exercise 4.1: Predict before running
Suppose we measure distance using annual income in dollars and number of late payments. Predict which feature will usually dominate before scaling. Would changing income from dollars to thousands of dollars affect the model? Explain why this is a problem even if the code runs.
Exercise 4.2: Find the leakage
A student imputes and scales the complete dataset, then calls train_test_split. A second student splits first, but scales all of X_train before calling cross_validate. Explain the leakage in each workflow and describe how to correct it.
Exercise 4.3: Choose a representation
For each feature, choose one-hot encoding, ordinal encoding, scaling, or no transformation. State any assumptions: (1) product weight in grams, (2) shirt size recorded as small, medium, or large, (3) Canadian province or territory, and (4) a numeric customer identifier.
Exercise 4.4: Trace a pipeline
A pipeline contains a median imputer, a standard scaler, and a \(k\)-NN classifier. Describe which methods are called on each component when one cross-validation fold is fit, when that fold is validated, and when predict is later called on deployment data. Which learned quantities remain unchanged?
Exercise 4.5: Check a mixed-feature workflow
A student runs preprocessor.fit_transform(X_train) and passes the result to cross_validate(KNeighborsClassifier(), ...). Explain why this still leaks validation information. Rewrite the workflow using a pipeline, and explain where each numeric and categorical transformer is fit. What happens to an input column that is not listed in the column transformer?
Summary
- Preprocessing transforms raw features into a representation a model can use.
- Scaling prevents numerical units from dominating distance-based models, while imputation handles missing values.
- Ordinal encoding represents meaningfully ordered categories; one-hot encoding avoids imposing order on nominal categories.
- Fit every transformer using only the available training data, including the training portion of each cross-validation fold.
- A
scikit-learnpipeline keeps preprocessing and prediction together, making leakage less likely. - A
ColumnTransformerapplies separate transformations to column groups and joins their outputs before prediction.