Named columns make pipelines debuggable and let downstream steps select features by name.
By default transformers return NumPy arrays (or sparse matrices), so column names are lost after the first step. Two features fix this.
get_feature_names_out() exists on every transformer, pipeline and ColumnTransformer, and returns the output column names, derived from the input names seen during fit.
set_output(transform="pandas") makes transform and fit_transform return a DataFrame with those names and the input's index. Call it on a single transformer, a pipeline (it propagates to all steps), or globally with sklearn.set_config(transform_output="pandas"). transform="polars" is supported too when polars is installed.
This pays off in several ways:
- You can inspect intermediate data with real names:
pipe[:-1].fit_transform(X)becomes a readable table. - A later
ColumnTransformercan select columns by name produced by an earlier step. - Mapping coefficients or importances back to features is trivial.
Limitations: pandas output needs dense data, so a OneHotEncoder must use sparse_output=False, and memory use rises with many categories. A transformer you write yourself needs get_feature_names_out (inheriting OneToOneFeatureMixin or ClassNamePrefixFeaturesOutMixin provides it) to take part.
Example
import pandas as pd
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
df = pd.DataFrame({"age": [20, 40, 60], "plan": ["a", "b", "a"]}, index=[10, 11, 12])
ct = make_column_transformer(
(StandardScaler(), ["age"]),
(OneHotEncoder(sparse_output=False), ["plan"]),
verbose_feature_names_out=False,
).set_output(transform="pandas")
out = ct.fit_transform(df)
print(out)
# age plan_a plan_b
# 10 -1.224745 1.0 0.0
# 11 0.000000 0.0 1.0
# 12 1.224745 1.0 0.0
print(type(out).__name__, list(ct.get_feature_names_out())) # DataFrame ['age', 'plan_a', 'plan_b']Remember
get_feature_names_out()names the output columns of any transformerset_output(transform="pandas")returns DataFrames, per object or globally- Pandas output requires dense data
Watch out for
- Enabling pandas output with a sparse OneHotEncoder, which raises an error.