Pipelines & feature engineering

ColumnTransformer, encoders, scaling, fitting transforms on train only. A Pipeline object is your first defense against train/serve skew.

About 18 min read3-question knowledge check

The mental model

A scikit-learn Pipeline is a sealed assembly line that carries its own calibration. Think of a commercial espresso machine: it grinds, tamps, and brews in the same fixed order every time — but the grind size and water temperature are dialled in once, against a reference batch of beans, and then applied identically to every future cup. You do not re-measure the beans on the counter and hand-tune each shot; the machine holds the settings.

That is exactly what a Pipeline does with data. Each step has two verbs: fit (learn state — column means, category vocabularies, IDF weights) and transform (apply that learned state). The one idea that prevents most production ML bugs: state is learned from the training rows only, then applied unchanged to validation, test, and live traffic. The mean you subtract from a live request is the training mean, computed months ago — not the mean of today's batch.

The reason this matters is **train/serve skew**, the single most common way a model that scored beautifully in a notebook falls apart in production. If preprocessing is a loose pile of pandas calls in a notebook cell, the serving code re-implements it by hand, and the two drift apart — a different imputation value, a category the encoder never saw, a scaler refit on the wrong data. A Pipeline object collapses "clean the data" and "predict" into one artifact that runs byte-identically in the notebook, in cross-validation, and behind the API. pipe.fit(X_train, y_train) and later pipe.predict(X_live) are the same transforms — that guarantee is the whole point.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision