Heterogeneous tabular preprocessing
Apply numeric scaling and categorical encoding in one preprocessing object.
Scikit-Learn · Function
Creates a ColumnTransformer from transformer-and-column pairs without requiring explicit transformer names. Names are generated automatically from transformer types.
The function accepts transformer-and-column pairs, then constructs a ColumnTransformer that applies each estimator to its selected subset and combines the outputs. Transformer names are generated automatically from estimator types, while remainder controls what happens to unspecified columns.
sklearn.compose.make_column_transformer(*transformers, remainder='drop', sparse_threshold=0.3, n_jobs=None, verbose=False, verbose_feature_names_out=True)| Name | Type | Description |
|---|---|---|
transformers | tuples | Tuples of (transformer, columns); transformer may be an estimator, 'drop', or 'passthrough', and columns identify the data subset. |
remainder | {'drop', 'passthrough'} or estimator, default='drop' | Controls processing of columns not listed in transformers: 'drop', 'passthrough', or an estimator; default is 'drop'. |
sparse_threshold | float, default=0.3 | Threshold used to decide whether the combined output is sparse; default is 0.3. |
n_jobs | int, default=None | Number of jobs used for fitting and transforming; default is None. |
verbose | bool, default=False | Whether to print progress messages; default is False. |
verbose_feature_names_out | bool, default=True | Whether generated feature names include transformer names; default is True. |
A ColumnTransformer object.
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
preprocessor = make_column_transformer(
(StandardScaler(), ["age"]),
(OneHotEncoder(handle_unknown="ignore"), ["city"]),
remainder="drop",
)
Creates a transformer that scales the numeric column and one-hot encodes the categorical column; other columns are discarded.
Apply numeric scaling and categorical encoding in one preprocessing object.
Keep columns not covered by explicit transformations.
Select feature groups based on names or data types.
It is a convenience wrapper around the ColumnTransformer constructor. It creates transformer names automatically from the types of the supplied estimators, so callers provide transformer-and-column tuples without explicit names.
Follow-up: What happens to columns omitted from those tuples? By default they are dropped; remainder='passthrough' retains them, or an estimator processes them.
Each tuple has the form (transformer, columns). The transformer can be an estimator, 'drop', or 'passthrough', and an estimator must support fit and transform.
Follow-up: How can selections be generalized? Use selectors such as make_column_selector to choose columns by name pattern or data type.
The function returns a ColumnTransformer object. That object applies the configured operations to the selected subsets and combines their results into one output.
Follow-up: How is output sparsity controlled? The returned ColumnTransformer exposes sparse_threshold, which determines whether a combined sparse result is returned.
ColumnTransformermake_column_selector