Course contentsShow
← Machine Learning and Deep Learning
Lesson 414 of 3,53810. Feature EngineeringFree lesson

Feature Scaling in Pipelines

Integrating scalers into sklearn pipelines to ensure consistent preprocessing across train/test splits.

Feature Scaling in Pipelines

What you'll learn: How to integrate scalers into sklearn pipelines so your preprocessing steps automatically apply correctly to both training and test data.

Why Pipelines Matter for Scaling

You've learned to fit scalers only on training data to avoid data leakage. But manually calling scaler.fit() and scaler.transform() on different datasets gets messy fast—especially when you add more preprocessing steps or need to apply the same workflow multiple times.

Pipelines solve this by bundling your scaler and model together. When you fit the pipeline, it automatically fits the scaler on training data, transforms it, then fits the model. When you predict on test data, it uses the already-fitted scaler to transform new data before prediction.

How It Works

Think of a pipeline as a recipe card. You write down the steps once: "First scale the ingredients, then bake the cake." Every time you follow that recipe, the scaling happens automatically before baking—you don't risk forgetting or doing it in the wrong order.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

# Create pipeline: scaling → model
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('classifier', LogisticRegression())
])

# Fit on training data (scaler fits, then model trains on scaled data)
pipeline.fit(X_train, y_train)

# Predict on test data (scaler transforms using training stats, then predicts)
y_pred = pipeline.predict(X_test)

The pipeline ensures your scaler is fitted once on X_train and those same parameters are reused for X_test—no manual bookkeeping required.

Key Takeaway: Pipelines automate the correct application of fitted scalers across train/test splits, preventing data leakage and ensuring your preprocessing workflow stays consistent and reproducible.