ColumnTransformer
A tool in scikit-learn for applying different preprocessing transformations to different columns of a data table, often used together with Pipeline to build modeling workflows for structured data.
Timeline (last 90 days)
ColumnTransformer is a tool provided by scikit-learn that allows transformers to be specified per column and automatically concatenates the results into a unified feature matrix.
When Pipeline is used together with ColumnTransformer, because Pipeline's fit only calls each step's fit on the training data and, in cross-validation, fits only on the training folds, data leakage can be effectively avoided.
ColumnTransformer's transformers parameter accepts a list whose elements are (name, transformer, column selection) tuples.
By default, ColumnTransformer discards columns not selected by any transformer; setting remainder='passthrough' preserves these columns, or a transformer can be specified to process the remaining columns.
ColumnTransformer supports the get_feature_names_out() method to obtain the transformed feature names, which helps with feature importance analysis or debugging.
OneHotEncoder returns a sparse matrix (scipy.sparse format) by default; when ColumnTransformer combines multiple transformers, the sparse_threshold parameter (default 0.3) controls the output format: if the sparsity (proportion of zero elements) of the concatenated matrix exceeds the threshold, a sparse matrix is output; otherwise, a dense array is output.
By setting OneHotEncoder(sparse_output=False) or adjusting ColumnTransformer's sparse_threshold parameter, one-hot encoding can be made to output a dense array.
ColumnTransformer can accept a list of column names, integer indices, a boolean mask, or a make_column_selector function as the column selection parameter.
For most tabular data projects, it is recommended to start building the preprocessing pipeline with ColumnTransformer rather than handling columns manually; the maintainability and reusability it brings far outweigh the initial learning cost.
All Facts (9)
ColumnTransformer is a tool provided by scikit-learn that allows transformers to be specified per column and automatically concatenates the results into a unified feature matrix.
50%UnverifiedWhen Pipeline is used together with ColumnTransformer, because Pipeline's fit only calls each step's fit on the training data and, in cross-validation, fits only on the training folds, data leakage can be effectively avoided.
50%UnverifiedColumnTransformer's transformers parameter accepts a list whose elements are (name, transformer, column selection) tuples.
50%UnverifiedBy default, ColumnTransformer discards columns not selected by any transformer; setting remainder='passthrough' preserves these columns, or a transformer can be specified to process the remaining columns.
50%UnverifiedColumnTransformer supports the get_feature_names_out() method to obtain the transformed feature names, which helps with feature importance analysis or debugging.
50%UnverifiedOneHotEncoder returns a sparse matrix (scipy.sparse format) by default; when ColumnTransformer combines multiple transformers, the sparse_threshold parameter (default 0.3) controls the output format: if the sparsity (proportion of zero elements) of the concatenated matrix exceeds the threshold, a sparse matrix is output; otherwise, a dense array is output.
50%UnverifiedBy setting OneHotEncoder(sparse_output=False) or adjusting ColumnTransformer's sparse_threshold parameter, one-hot encoding can be made to output a dense array.
50%UnverifiedColumnTransformer can accept a list of column names, integer indices, a boolean mask, or a make_column_selector function as the column selection parameter.
50%UnverifiedFor most tabular data projects, it is recommended to start building the preprocessing pipeline with ColumnTransformer rather than handling columns manually; the maintainability and reusability it brings far outweigh the initial learning cost.
50%