Scikit-learn pipelines are workflow tools that chain preprocessing transformers and estimators into a single composable object. Located in sklearn.pipeline and sklearn.compose, they ensure reproducible data transformations, prevent data leakage during cross-validation, and streamline hyperparameter tuning. Pipelines enforce that each transformation step learned from training data (scaling means, encoding categories) is applied identically to validation and test folds. Key mental model: think of pipelines as assembly lines where each station (transformer) modifies the data in a consistent, repeatable way β transformers never see test data during fit, only during transform.
What This Cheat Sheet Covers
This topic spans 13 focused tables and 58 indexed concepts, 55 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Pipeline Classes
These are the building blocks you compose everything else from. Pipeline chains steps in sequence, ColumnTransformer routes different columns to different transformers, and FeatureUnion runs transformers in parallel and stitches their outputs together β with make_* shortcuts that auto-name steps so you can skip the boilerplate tuples. Master these five and the rest of the library snaps into place around them.
| Class | Example | Description | |
|---|---|---|---|
Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression())]) | β’ Chains transformers sequentially with an optional final estimator β’ calls fit_transform on each step except last | ||
make_pipeline(StandardScaler(), LogisticRegression()) | Convenience constructor that auto-generates step names ( 'standardscaler', 'logisticregression') instead of requiring tuples | ||
ColumnTransformer([('num', StandardScaler(), ['age', 'income']), ('cat', OneHotEncoder(), ['city'])]) | β’ Applies different transformers to different column subsets β’ concatenates results horizontally into single feature matrix | ||
make_column_transformer((StandardScaler(), ['age', 'income']), (OneHotEncoder(), ['city'])) | Convenience constructor for ColumnTransformer that omits need to name each transformer tuple | ||
FeatureUnion([('pca', PCA(n_components=2)), ('poly', PolynomialFeatures(degree=2))]) | β’ Applies multiple transformers in parallel to the same input data β’ concatenates outputs horizontally | ||
FunctionTransformer(np.log1p, inverse_func=np.expm1) | Wraps arbitrary Python function as a transformer with optional inverse_transform support for reversible operations |