StandardScaler
scikit-learn
Pipeline
Python
machine learning

How to apply StandardScaler in Pipeline in scikit-learn sklearn?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

StandardScaler should usually be applied inside a scikit-learn Pipeline, not on the full dataset ahead of time. Putting scaling in the pipeline ensures the scaler is fit only on training folds, which prevents data leakage and keeps training and inference preprocessing consistent.

Why Pipeline matters

StandardScaler computes a mean and standard deviation from the data it sees. If you fit it on the full dataset before splitting, information from validation or test data leaks into the training process.

The correct pattern is:

  1. split the data
  2. fit the pipeline on training data
  3. let the pipeline scale and predict on new data

That keeps the scaler statistics tied to the training set only.

Basic pipeline example

A simple numeric classification workflow looks like this:

python
1from sklearn.datasets import load_wine
2from sklearn.model_selection import train_test_split
3from sklearn.pipeline import Pipeline
4from sklearn.preprocessing import StandardScaler
5from sklearn.linear_model import LogisticRegression
6
7X, y = load_wine(return_X_y=True)
8
9X_train, X_test, y_train, y_test = train_test_split(
10    X, y, test_size=0.2, random_state=42, stratify=y
11)
12
13model = Pipeline([
14    ("scaler", StandardScaler()),
15    ("clf", LogisticRegression(max_iter=2000)),
16])
17
18model.fit(X_train, y_train)
19print("test accuracy:", model.score(X_test, y_test))

The scaler is fit during model.fit, and the same fitted scaler is reused automatically during score, predict, and predict_proba.

When scaling helps most

StandardScaler is especially helpful for models that depend on feature scale:

  • logistic regression
  • linear models with regularization
  • support vector machines
  • k-nearest neighbors
  • neural networks

It is usually less important for tree-based methods such as random forests and gradient-boosted trees, because tree splits are not based on Euclidean distance or comparable coefficient magnitudes.

Inspect intermediate transformed values

Sometimes you want to confirm that the scaler is behaving as expected.

python
1from sklearn.pipeline import Pipeline
2from sklearn.preprocessing import StandardScaler
3from sklearn.linear_model import LogisticRegression
4
5pipe = Pipeline([
6    ("scaler", StandardScaler()),
7    ("clf", LogisticRegression(max_iter=1000)),
8])
9
10pipe.fit(X_train, y_train)
11
12X_train_scaled = pipe.named_steps["scaler"].transform(X_train)
13print(X_train_scaled.mean(axis=0)[:3])
14print(X_train_scaled.std(axis=0)[:3])

The transformed training columns should be close to zero mean and unit variance, aside from floating-point effects.

Use ColumnTransformer for mixed data

Real datasets often contain both numeric and categorical columns. In that case, scale only the numeric columns and leave categorical preprocessing separate.

python
1import pandas as pd
2from sklearn.compose import ColumnTransformer
3from sklearn.impute import SimpleImputer
4from sklearn.pipeline import Pipeline
5from sklearn.preprocessing import OneHotEncoder, StandardScaler
6from sklearn.linear_model import LogisticRegression
7
8df = pd.DataFrame({
9    "age": [24, 30, 41, 35],
10    "income": [50000, 65000, 80000, 72000],
11    "city": ["A", "B", "A", "C"],
12    "y": [0, 1, 1, 0],
13})
14
15X = df[["age", "income", "city"]]
16y = df["y"]
17
18numeric_features = ["age", "income"]
19categorical_features = ["city"]
20
21numeric_pipe = Pipeline([
22    ("imputer", SimpleImputer(strategy="median")),
23    ("scaler", StandardScaler()),
24])
25
26categorical_pipe = Pipeline([
27    ("imputer", SimpleImputer(strategy="most_frequent")),
28    ("onehot", OneHotEncoder(handle_unknown="ignore")),
29])
30
31preprocess = ColumnTransformer([
32    ("num", numeric_pipe, numeric_features),
33    ("cat", categorical_pipe, categorical_features),
34])
35
36model = Pipeline([
37    ("preprocess", preprocess),
38    ("clf", LogisticRegression(max_iter=1000)),
39])

This is the production-friendly pattern because each column type gets the right transformation.

Make cross-validation safe automatically

Pipelines also help with cross-validation and grid search because each fold gets its own scaler fit.

python
1from sklearn.model_selection import cross_val_score
2
3scores = cross_val_score(model, X_train, y_train, cv=5)
4print(scores.mean())

Without the pipeline, it is easy to scale once globally and accidentally leak fold information into the evaluation.

Common Pitfalls

The most common mistake is fitting StandardScaler on the entire dataset before the split, which leaks information and inflates validation metrics. Another is scaling all columns blindly, including categorical features that should be one-hot encoded instead. Developers also sometimes use scaling with tree-based models and expect a major accuracy improvement that is unlikely to appear. Forgetting that sparse text matrices should usually not be centered with the default scaler behavior is another practical issue in other workflows. Finally, many people build the scaler outside the pipeline and then forget to apply the same transformation consistently at prediction time.

Summary

  • Put StandardScaler inside a Pipeline so fitting stays training-only and leak-free.
  • Use it mainly for scale-sensitive estimators such as linear models, SVMs, and k-nearest neighbors.
  • Inspect named_steps when you need to debug transformed values.
  • Use ColumnTransformer for mixed numeric and categorical data.
  • Keep cross-validation and grid search inside the pipeline workflow.
  • Treat the pipeline as the full preprocessing-plus-model contract, not just a convenience wrapper.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.