liveBook · Manning

ColumnTransformer

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

ColumnTransformer is a scikit-learn class that applies different transformations to specified subsets of input features and combines their outputs.

ColumnTransformer is a scikit-learn preprocessing component for tabular data with mixed feature types. It assigns a transformer, or a small transformation pipeline, to each selected group of columns and combines the resulting outputs into one model-ready feature matrix. Select columns by name or position; leave them unchanged, transform them, or exclude them. section 4.1.3 section 4.2.2

Use it when categorical and numeric fields need different preparation. A common arrangement one-hot encodes low-cardinality categories while imputing numeric missing values; another standardizes numeric fields after imputation. One-hot encoding can expand a categorical field into multiple binary features, so the transformed output may have more columns than the input. section 4.2.2

Configure each transformation branch as a tuple containing a branch name, a transformer, and its columns:

  • Apply OneHotEncoder to selected low-cardinality categorical fields. Set handle_unknown='ignore' when unseen categories should not interrupt transformation. section 4.2.2 section 7.2.2
  • Apply an imputer or a numeric pipeline to continuous fields; use 'passthrough' when a model can receive numeric values without further processing. section 4.2.2 section 7.2.2
  • Set remainder='drop' to discard every column not explicitly assigned to a branch. Set verbose_feature_names_out=False for shorter output names, and sparse_threshold=0.0 when a dense array is required. section 5.1
  • Place the transformer before an estimator in a Pipeline so fitting, transformation, prediction, and evaluation use the same preparation steps. section 4.1.3 section 4.3.1

Preparing Airbnb listing features

For Airbnb NYC listings, a transformer one-hot encodes neighbourhood_group and room_type, imputes continuous fields with zero, and drops unlisted columns. Running fit_transform produces a float NumPy array with 13 columns, reflecting the binary features created for categorical values. The resulting transformer can be the processing stage before linear regression or a decision-tree classifier. section 4.2.2 section 4.3.1 section 5.1

Transformation choices should match both the feature and the model. In an XGBoost workflow, one feature is one-hot encoded, other categorical features receive ordinal or target encoding, and numeric features pass through unchanged. The branches also specify behavior for unknown categories and missing values at test time; transformed feature names reveal which branch produced each output column. section 7.2.2

column_transform = ColumnTransformer(
                [('categories', 
                  categorical_onehot_encoding, 
                  low_card_categorical),              #1
                 ('numeric', 
                  numeric_passthrough, 
                  continuous),                        #2
                ],
    remainder='drop',                                 #3
    verbose_feature_names_out=False,                  #4
    sparse_threshold=0.0                              #5
)

Inspect a compact ColumnTransformer definition that maps categorical and continuous column lists to separate branches, drops everything else, and requests dense, concise output. section 4.2.2

Listing 7.13 Defining column transformations
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
 
from category_encoders.target_encoder import TargetEncoder
 
onehot_encoder = OneHotEncoder(handle_unknown='ignore')          #1
ordinal_enconder = OrdinalEncoder(handle_unknown="use_encoded_value",
                                  unknown_value=np.nan)          #2
target_encoder = TargetEncoder(
    cols=target_encoding, 
    handle_unknown="value", 
    smoothing=0.5
)                                                                #3
 
column_transform = ColumnTransformer(
    [('onehot_encoding', onehot_encoder, onehot_encoding),
     ('ordinal_encoding', ordinal_enconder, ordinal_encoding),
     ('target_encoding', target_encoder, target_encoding),
     ('numeric', 'passthrough', numeric),
     ('binary', 'passthrough', binary)],                         #4
    remainder='drop',                                            #5
    verbose_feature_names_out=True,                              #6
    sparse_threshold=0.0)                                        #7

Compare a multi-branch configuration combining one-hot, ordinal, and target encoding with numeric pass-through for an XGBoost pipeline. section 7.2.2

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

How deep it goes
Defines branch tuples, column selection, output options, and pipeline use without descending into implementation internals.
Examples
Uses Airbnb listings to one-hot encode categories, process continuous fields, train linear and tree models, and assemble XGBoost features.
What you can do after
Apply it via scikit-learn to build reproducible preprocessing-and-model pipelines for mixed tabular data.
How it connects
Connects Pipeline, OneHotEncoder, OrdinalEncoder, SimpleImputer, StandardScaler, cross-validation, and XGBoost.

Also explained in

Compare how 2 books explain ColumnTransformer →

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.