How to extract feature importances from an 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
Reading feature importance from a scikit-learn pipeline is easy only when the pipeline does no preprocessing. In real projects, preprocessing steps such as one-hot encoding or scaling change the shape of the data before it reaches the model. To get meaningful importances, you need the fitted estimator and the transformed feature names that correspond to what the estimator actually saw.
Get the Final Estimator from the Pipeline
In scikit-learn, the last step in a pipeline is usually the trained model. You can access it through named_steps.
After fitting, the model is:
That gives you numeric values only. It does not tell you which transformed columns those numbers belong to.
Recover Feature Names After Preprocessing
If the pipeline uses a ColumnTransformer, call get_feature_names_out() on the preprocessing step after fitting.
This is the standard pattern for tree-based models inside a preprocessing pipeline.
Know Which Models Support Native Importance
Not every estimator exposes feature_importances_. Tree models usually do, but many others do not.
Typical cases:
- random forest, gradient boosting, and extra trees expose
feature_importances_ - linear and logistic models expose
coef_ - some models expose neither and require a model-agnostic method
If you are using logistic regression, inspect coefficients instead:
Coefficients and tree importances are not interchangeable, so do not compare them as if they mean exactly the same thing.
Use Permutation Importance for Model-Agnostic Interpretation
If the final estimator does not expose native importance values, permutation importance is often the safest fallback. It measures how much model performance drops when a feature is shuffled.
This works with the full pipeline object, which is useful because the pipeline handles preprocessing internally during scoring.
Watch for Feature Expansion
One raw input column does not always stay one column after preprocessing. One-hot encoding can turn a single categorical feature into several transformed columns. For example, city can become city_A, city_B, and city_C.
That means there are two different interpretation levels:
- transformed-feature importance
- original-feature importance
If you need the original level, aggregate related transformed columns yourself. For instance, you might sum all importance values whose names begin with the same source feature prefix.
Common Pitfalls
- Reading
feature_importances_without recovering the transformed feature names. - Assuming every estimator inside a pipeline exposes
feature_importances_. - Confusing linear model coefficients with tree-based importance values.
- Forgetting that one categorical source column may expand into many transformed columns.
- Trying to interpret an unfitted pipeline and getting missing attribute errors.
Summary
- Feature importance from a pipeline only makes sense if you map it to the transformed feature set.
- Use
named_stepsto access the fitted estimator. - Use
get_feature_names_out()on preprocessing steps such asColumnTransformer. - Use
coef_for linear models and permutation importance for model-agnostic cases. - Distinguish between transformed-column importance and original-feature importance before reporting results.
Related reading
- How to extract human voice from an audio clip, using machine learning?
- How to extract sklearn decision tree rules to pandas boolean conditions?
- How to extract the decision rules from scikit-learn decision-tree?
- How to extract the decision rules from scikit-learn decision-tree?
- How to feed input with changing size in Tensorflow
- How to filter for rows with close values across columns
- How to extract unsupervised clusters from a Dirichlet Process in PyMC3?
- How to feed back `RNN` output to input in tensorflow
.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.