liveBook · Manning

Scikit-Learn pipeline

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

scikit-learn Pipeline is a composite estimator that chains named data transformations and a final estimator into one sequential modeling object.

A Scikit-Learn Pipeline packages ordered preprocessing steps and a final model as one object. Name each step when creating it, then use the usual fit and transform or predict API. The pipeline runs every step in sequence, while still allowing inspection and tuning of individual steps. section 4.1.3

Pipelines make tabular modeling repeatable: data preparation and prediction stay together, and transformation parameters can be handled alongside model parameters. When columns need different treatment, use ColumnTransformer inside the pipeline; when separate transformation sequences must be combined, FeatureUnion is another option. section 4.1.3

A practical tabular-data workflow is:

  • Create reusable preprocessing blocks, such as one-hot encoding for categorical values, zero imputation for numeric values, and standardization after imputation. section 4.2.2
  • Use ColumnTransformer to apply those blocks to selected column groups simultaneously. section 4.2.2
  • Wrap the column transformer and estimator in a Pipeline, then evaluate the complete workflow with cross_validate and cross-validation folds. section 4.3.1

Airbnb price regression workflow

For Airbnb NYC data, a ColumnTransformer one-hot encodes low-cardinality categorical features and imputes numeric features. A Pipeline then runs that processing step before LinearRegression. Cross-validation records predictive performance, timing, and trained models; the fitted pipeline also provides access to model coefficients and weights. section 4.3.1

Listing 4.3 Linear regression
from sklearn.linear_model import LinearRegression
from sklearn.metrics import make_scorer, mean_squared_error
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_validate
 
column_transform = ColumnTransformer(
    [('categories', categorical_onehot_encoding, low_card_categorical),
     ('numeric', numeric_passthrough, continuous)],
    remainder='drop',
    verbose_feature_names_out=False,
    sparse_threshold=0.0)                                   #1
 
model = LinearRegression()                                  #2
 
model_pipeline = Pipeline(
    [('processing', column_transform),
     ('modeling', model)]                                   #3
)
 
cv = KFold(5, shuffle=True, random_state=0)                 #4
rmse =  make_scorer(mean_squared_error, 
                    squared=False)                          #5
 
cv_scores = cross_validate(estimator=model_pipeline, 
                           X=data[price_window], 
                           y=target_regression[price_window],
                           scoring=rmse,
                           cv=cv, 
                           return_train_score=True,
                           return_estimator=True)           #6
 
mean_cv = np.mean(cv_scores['test_score'])
std_cv = np.std(cv_scores['test_score'])
fit_time = np.mean(cv_scores['fit_time'])
score_time = np.mean(cv_scores['score_time'])
print(f"{mean_cv:0.3f} ({std_cv:0.3f})", 
      f"fit: {fit_time:0.2f}", 
      f"secs pred: {score_time:0.2f} secs")                 #7

This listing shows the essential composition: a column-processing step followed by a modeling step, evaluated with KFold cross-validation and RMSE. section 4.3.1

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains step naming, sequential execution, parameter access, column-specific preparation, and cross-validation, without detailing Pipeline internals.
Examples
Demonstrates Airbnb NYC preprocessing and LinearRegression with KFold, RMSE, and a 13-column transformed feature array.
What you can do after
Apply it via Scikit-Learn to package column transformations and a model, then evaluate the complete workflow with cross-validation.
How it connects
Connects Pipeline with ColumnTransformer, FeatureUnion, one-hot encoding, imputation, standardization, LinearRegression, and cross-validation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.