After one-hot encoding inside a ColumnTransformer, I can't map the selector's output back to the original feature names.
Why this is tricky
The selector doesn't see your original columns. It sees the output of the preprocessing step, which may have many more columns (from one-hot encoding) in a different order. You need the names of the preprocessed columns, then the selector's mask on top of those.
Answer 1: Slice the pipeline up to the selector
A Pipeline can be sliced like a list. pipe[:-1] is everything except the final model. Calling get_feature_names_out() on that slice runs the names through every step, including the selector.
pipe = Pipeline([
("prep", preprocess), # a
ColumnTransformer
("select", SelectKBest(f_classif, k=10)),
("clf", LogisticRegression(max_iter=1000)),
])
pipe.fit(X, y)
print(pipe[:-1].get_feature_names_out())
# ['cat__city_Delhi', 'num__age', ...]
The prefixes (cat__, num__) come from the ColumnTransformer's step names. Use verbose_feature_names_out=False in the ColumnTransformer if you don't want them.
