show feature names after feature selection
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
After feature selection, the transformed matrix usually has fewer columns, but the model output no longer tells you which original features survived. In scikit-learn, the standard way to recover those names is to use the selector's support mask or, in newer APIs, get_feature_names_out.
The Core Pattern
Feature selectors keep track of which columns were retained. That means you can fit the selector once, then ask for either:
- a Boolean mask with
get_support() - selected names with
get_feature_names_out()
If your input started as a pandas DataFrame, this becomes straightforward.
This works across many selectors because get_support() is part of the selector interface.
Using get_feature_names_out
Many modern scikit-learn selectors also expose get_feature_names_out, which is often cleaner:
That saves you from manually applying the mask.
If the estimator was fitted on a DataFrame with string column names, many selectors also remember feature_names_in_, so this can work:
Still, passing the original column list explicitly is often the least surprising option.
Pipelines and Preprocessing
The task gets more interesting when feature selection happens after one-hot encoding or other transformations. Then the relevant names are no longer the raw DataFrame columns. You first need the post-preprocessing feature names, and only then can the selector mask them.
Example with ColumnTransformer and a selector:
This is the pattern to remember: ask the preprocessor for names first, then pass those names into the selector.
Why This Matters
Being able to print selected names is not just for curiosity. It helps with:
- model interpretability
- debugging leakage or redundant features
- documenting what the pipeline actually learned
- stable downstream reporting
Without names, feature selection just produces anonymous columns.
Common Pitfalls
The most common mistake is indexing the original DataFrame columns after one-hot encoding. Once preprocessing expands or reorders features, the original names no longer match the transformed matrix.
Another mistake is assuming every selector exposes importances directly. Some selectors only expose a support mask, which is enough to recover names but not always enough to rank them.
A third issue is mixing NumPy arrays and DataFrames too early. If you drop column names before fitting, name recovery becomes harder because you have to track them manually.
Summary
- Use
get_support()to build a mask over the original feature names. - Prefer
get_feature_names_out()when the selector supports it. - In pipelines, get names from the preprocessor first, then apply the selector.
- Keep DataFrame column names around as long as possible for easier debugging.
- Feature-name recovery is especially important after one-hot encoding and other expanding transforms.

