Sub topic 12
Feature importance and its caveats
This chapter assumes you can fit a scikit-learn tree and that you know Gini impurity and weighted impurity decrease from the earlier chapters. It covers how a tree turns its splits into one importance score per feature, and why you should be careful when reading those scores.
How the score is computed
A fitted tree records how much each split reduced impurity. For every internal node, scikit-learn calculates:
- the node's impurity, minus the weighted impurity of its two children
- multiplied by the fraction of training samples that reached the node
It adds these values for each feature, then divides by the total so the scores sum to 1. This is often called mean decrease in impurity (MDI), or Gini importance.
A feature used in a high split that affects many samples can score high even if it is used only once. A feature that is never used for a split scores exactly 0.
Reading the scores in code
The values are available as feature_importances_. The script below puts them next to permutation importance. Permutation importance shuffles one feature at a time and measures how much the score drops on held-out data.
from sklearn.datasets import load_iris
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.3, random_state=0, stratify=data.target
)
clf = DecisionTreeClassifier(max_depth=3, random_state=0)
clf.fit(X_train, y_train)
perm = permutation_importance(
clf, X_test, y_test, n_repeats=30, random_state=0
)
for name, mdi, pimp in zip(
data.feature_names, clf.feature_importances_, perm.importances_mean
):
print(f"{name:20s} impurity={mdi:.3f} permutation={pimp:.3f}")The first column comes from training data and the structure of the tree. The second column comes from test data and measures how much the model's predictions depend on the feature.
Caveats
| Caveat | What goes wrong |
|---|---|
| Computed on training data | A deep tree that memorizes noise gives credit to features that do not generalize. |
| Bias toward many split points | Continuous or high-cardinality features have more candidate thresholds, so they win splits by chance more often. |
| Correlated features | The tree picks one of two near-duplicates, often arbitrarily. That one gets most of the credit and the other looks unimportant. |
| Instability | A small change in the training data can change the top split and reshuffle the scores. |
| No direction or causation | A high score says the feature was useful for splitting. It does not say whether increasing the feature raises or lowers the prediction, or that it causes the outcome. |
Practical habits
- Compute importance only on a tree you have already limited with stopping rules or pruning. Otherwise noise features inflate the result.
- Compare MDI with permutation importance on held-out data. If they disagree strongly, trust the held-out measure more.
- Refit on several bootstrap samples or cross-validation folds and check whether the ranking stays stable.
- Before concluding that a feature is unimportant, check whether it is strongly correlated with another feature. Permuting one of a correlated pair can also understate its value, because the model can still use the other.
- Use the tree's text rules to see how a feature is used, since the importance score hides thresholds and direction.
Exercise
A tree gives feature A an importance of 0.62 and feature B an importance of 0.00. Features A and B are almost perfectly correlated. Can you conclude that B carries no information about the target? Give one check you would run.
Show answer
No. B may carry the same information as A, and the tree simply chose A for its splits. To check, refit the tree without A and see whether B now gets a high importance, or refit on bootstrap samples and see whether the credit switches between A and B.