machine learning
sklearn pipeline
feature importances
data preprocessing
model interpretability

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.

Practice ML system design

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.

python
1from sklearn.pipeline import Pipeline
2from sklearn.preprocessing import StandardScaler
3from sklearn.ensemble import RandomForestClassifier
4
5pipe = Pipeline([
6    ("scale", StandardScaler()),
7    ("model", RandomForestClassifier(random_state=0)),
8])

After fitting, the model is:

python
model = pipe.named_steps["model"]
print(model.feature_importances_)

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.

python
1import pandas as pd
2from sklearn.compose import ColumnTransformer
3from sklearn.pipeline import Pipeline
4from sklearn.preprocessing import OneHotEncoder, StandardScaler
5from sklearn.ensemble import RandomForestClassifier
6
7X = pd.DataFrame(
8    {
9        "age": [25, 42, 31, 52],
10        "income": [40000, 90000, 60000, 120000],
11        "city": ["A", "B", "A", "C"],
12    }
13)
14y = [0, 1, 0, 1]
15
16prep = ColumnTransformer(
17    transformers=[
18        ("num", StandardScaler(), ["age", "income"]),
19        ("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
20    ]
21)
22
23pipe = Pipeline([
24    ("prep", prep),
25    ("model", RandomForestClassifier(random_state=0)),
26])
27
28pipe.fit(X, y)
29
30feature_names = pipe.named_steps["prep"].get_feature_names_out()
31importances = pipe.named_steps["model"].feature_importances_
32
33importance_table = pd.DataFrame(
34    {"feature": feature_names, "importance": importances}
35).sort_values("importance", ascending=False)
36
37print(importance_table)

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:

python
1from sklearn.linear_model import LogisticRegression
2
3pipe = Pipeline([
4    ("prep", prep),
5    ("model", LogisticRegression(max_iter=1000)),
6])
7
8pipe.fit(X, y)
9
10feature_names = pipe.named_steps["prep"].get_feature_names_out()
11coefs = pipe.named_steps["model"].coef_[0]
12
13coef_table = pd.DataFrame(
14    {"feature": feature_names, "coefficient": coefs}
15).sort_values("coefficient", ascending=False)
16
17print(coef_table)

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.

python
1from sklearn.inspection import permutation_importance
2
3result = permutation_importance(pipe, X, y, n_repeats=10, random_state=0)
4
5perm_table = pd.DataFrame(
6    {"feature": X.columns, "importance": result.importances_mean}
7).sort_values("importance", ascending=False)
8
9print(perm_table)

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_steps to access the fitted estimator.
  • Use get_feature_names_out() on preprocessing steps such as ColumnTransformer.
  • 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.