How to get feature names selected by feature elimination in sklearn pipeline?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When Recursive Feature Elimination sits inside a scikit-learn pipeline, the selected features are not exposed automatically as a friendly list of names. The usual solution is to get the transformed feature names from the preprocessing step, then apply the selector's boolean support mask to those names.
The Core Idea
An RFE-style selector stores which features survived in support_. That array is boolean and matches the feature matrix seen by the selector. So the workflow is:
- fit the pipeline
- get the feature names entering the selector
- apply
support_to those names
That works whether the selector is RFE, RFECV, or another transformer exposing the same mask semantics.
Example with a Simple Pipeline
In this simple case, the input feature names are just the original DataFrame column names because the scaler keeps the same feature count and order.
When the Pipeline Changes Feature Names
Real pipelines often include transformations such as one-hot encoding. In that case, the names entering RFE are not the original column names anymore. You need the transformed names from the preprocessing stage.
This is the general pattern you want in modern scikit-learn pipelines.
Why the Support Mask Is the Right Tool
RFE recursively removes features until only the requested number remains. The final result is stored in:
- '
support_for selected-versus-dropped' - '
ranking_for relative importance order'
If you only want the chosen feature names, support_ is the direct answer.
If you want a fuller diagnostic view, ranking_ can be paired with the transformed feature names too.
A Reusable Helper
The exact helper shape is up to you, but packaging the pattern once makes repeated model inspection easier.
Common Pitfalls
The biggest pitfall is applying support_ to the original DataFrame columns when the preprocessing step changed the feature space. After one-hot encoding or similar transforms, the selector is no longer working on the raw column list.
Another common mistake is reading support_ before fitting the pipeline. The selector mask does not exist until the estimator has been trained.
People also confuse ranking_ with the final selected mask. ranking_ == 1 usually means selected, but support_ is the clearer and safer attribute for this purpose.
Summary
- Fit the pipeline first, then read the selector's
support_mask. - Apply that mask to the feature names that actually enter the selector.
- If preprocessing changes the feature space, use
get_feature_names_out()from the preprocessing step. - Use
support_for selected features andranking_when you want broader elimination diagnostics. - The general solution is "transformed feature names plus selector mask."
Related reading
- How to get Graph or GraphDef from a given Model?
- How to get inertia value for each k-means cluster using scikit-learn?
- How to get labels ids in Keras when training on multiple classes?
- How to get mini-batches in pytorch in a clean and efficient way?
- How to get Grafana to include sum of values in tooltip or legend for stacked linechart
- How to get largest number of consecutive integers in a substantially large array (spread across multiple machines)
- How to get first element in a list of tuples?
- How to get GET request values in Django?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.