Feature Importance in Tree Models
A decision tree learns a hierarchy of if/else questions that leads to a decision.
A Game of 20 Questions
Decision trees are widely used models for classification and regression tasks. At their core, a decision tree learns a hierarchy of if/else questions, and the answers to those questions lead to a decision. This article follows that idea from an everyday intuition to the way the model is built, tested, and inspected.
The intuition starts with a familiar game. Imagine you need to distinguish between four animals: bears, hawks, penguins, and dolphins. Your goal is to reach the right answer by asking as few if/else questions as possible. Before reading on, try to pick a first question that would be useful. Which single yes/no question would cut the four animals down the most?
Tracing the Animal Tree
A good first question is whether the animal has feathers, because that narrows the possibilities to just two animals. If the answer is yes, you can ask whether the animal can fly, which separates hawks from penguins. If the answer is no, the remaining choices are dolphins and bears, and you can ask whether the animal has fins to tell them apart.
Trace a single animal through the tree. Suppose the animal has feathers and cannot fly. You start at the root, answer yes to the feather question, then answer no to the flight question, and you arrive at the leaf for penguin. Each path from the root to a leaf is one complete sequence of questions that leads to a decision.
A node is either a question or a terminal node, also called a leaf, that contains the answer. The edges connect each answer to the next question you would ask.
In machine learning terms, this tree is a model that distinguishes four classes of animals using three features: has feathers, can fly, and has fins. Instead of building such a model by hand, we can learn it from data using supervised learning. The learning task is to discover which questions to ask and in what order.
How the Algorithm Chooses a Test
Learning a decision tree means learning the sequence of if/else questions that gets to the true answer most quickly. In the machine learning setting these questions are called tests. Real data is usually made of continuous features rather than yes/no features like the animal example, so the tests take the form 'Is feature i larger than value a?'
To build a tree, the algorithm searches over all possible tests and selects the one that is most informative about the target variable. The source illustrates this with a two-dimensional dataset of two half-moon shapes, called two_moons, containing 75 points per class. For that dataset, the first chosen test is x[1] <= 0.0596, which best separates the points of class 1 from the points of class 2.
The top node, called the root, represents the whole dataset, which holds 75 points of class 0 and 75 points of class 1. After the first split, the left node holds 2 points of class 0 and 32 of class 1, and the right node holds 48 points of class 0 and 18 of class 1. Follow those counts in the diagram below and notice what the split achieves and what it leaves unfinished.
The first split separates the classes well, but each side still contains some points of the other class. The algorithm therefore repeats the search for the best test inside each region. For the two-moons data, the most informative next split for both the left and right regions is based on x[0]. Each test concerns only a single feature, so every boundary in the partition is parallel to one of the axes.
Splitting Until Leaves Are Pure
The process of searching for a test and splitting a region repeats recursively. This yields a binary tree of decisions in which each node contains a test that splits the part of the data currently under consideration. The recursion stops when each region, which is each leaf of the tree, contains only a single target value, either a single class or a single regression value.
A prediction for a new data point is made by checking which region of the partition the point lies in. The tree is traversed from the root, going left or right depending on whether each test is fulfilled, until a leaf is reached. The prediction is the majority target in that region, or the single target when the leaf is pure. For regression, the same traversal applies, but the output is the mean target of the training points in the leaf the point falls into.
Overfitting and Its Two Remedies
If you keep building the tree until all leaves are pure, the model becomes very complex and highly overfit to the training data. Pure leaves mean the tree is 100 percent accurate on the training set, because each training point sits in a leaf whose majority class is correct. The source shows that the decision boundary of such a tree focuses heavily on single outlier points that are far from the other points of their class, which is not how one would expect a sensible boundary to look.
The source gives two common strategies to prevent overfitting. The first is to stop creating the tree early, which is called pre-pruning. The second is to build the full tree and then remove or collapse nodes that contain little information, which is called post-pruning or just pruning.
| Strategy | When it acts | What it does |
|---|---|---|
| Pre-pruning | During growth | Stops creating the tree early, before all leaves are pure |
| Post-pruning | After growth | Builds the full tree, then removes or collapses nodes that contain little information |
The two strategies for preventing overfitting in decision trees
Reading the Depth Comparison
The source compares a default tree with one whose depth is limited. In scikit-learn, the depth limit is set through the max_depth parameter. Two scores are compared: the training accuracy, measured on the data the tree learned from, and the test accuracy, measured on held-out data. The example is described in words here, so the focus stays on what each number means.
Interpreting the default tree
A default tree on the Breast Cancer data is trained until every leaf is pure. Its training accuracy is 1.000 and its test accuracy is 0.937. What does this pattern suggest, and what should you check first?
- 1Identify the training score
A training accuracy of 1.000 means the tree classifies every training point correctly. The source predicts this for a tree grown deep enough to memorize all the training labels.
- 2Identify the test score
A test accuracy of 0.937 is lower than the training score. The source notes it is also slightly worse than the linear models it examined earlier, which reached around 95 percent.
- 3Compare the two scores
A high training score paired with a lower test score points to overfitting in the tree's structure.
- 4Find the changed setting
When the depth-limited run is compared with the default, the depth limit is the setting that changed between the two runs, so it is the first place to look.
The gap between training and test accuracy signals overfitting. Limiting max_depth lowered training accuracy and raised test accuracy in the source example, which is the expected trade-off of pre-pruning.
| Tree setting | Training accuracy | Test accuracy |
|---|---|---|
| Default, grown until leaves are pure | 1.000 | 0.937 |
| Depth limited with max_depth | Lower than the default | Higher than the default |
Direction of change reported in the source example. Exact depth-limited values are not given in the source.
Summarizing a Tree with Feature Importance
Instead of reading the whole tree, you can summarize it with feature importance. Feature importance rates how important each feature is for the tree's decisions. It is a number between 0 and 1 for each feature, where 0 means the feature was not used at all and 1 means it perfectly predicts the target. The feature importances always sum to 1, so they describe how the tree's decision-making is shared across features.
On the Breast Cancer tree, the feature used in the top split, worst radius, is by far the most important. This matches the observation from the tree visualization that the first level already separates the two classes fairly well. The top split is therefore a good place to look when you want to know which feature drives the model most.
What do you think happens?
If worst radius has a high feature importance, can you tell whether a high worst radius points toward benign or malignant samples?
Reveal answer
No, importance does not indicate direction
Feature importances are always positive and do not indicate which class a feature points toward. They tell you that worst radius matters, but not whether a high radius is associated with benign or malignant samples.
Mistakes When Reading Trees
Common mistake Treating a high training score as proof of a good model
What it looks likeA default tree reports training accuracy of 1.000 and a learner concludes the model is ready to use.
Why it failsA tree grown until all leaves are pure memorizes the training labels, so the training score says little about new data.
Do this insteadCompare the training score with the test score and treat a large gap as a sign of overfitting.
Common mistake Reading feature importance as the direction of an effect
What it looks likeWorst radius has high importance, so a learner assumes large radii always mean malignant.
Why it failsImportance values are always positive and do not say which class a feature points toward.
Do this insteadUse importance to rank which features matter, then examine the data or the tree's splits to learn the direction.
Common mistake Assuming the first split is the only split that matters, when later splits repeat the search inside each region
Common mistake Forgetting that a leaf is only pure when all of its points share one target value
Practice: Trace a New Point
Using the animal tree, trace an animal that has no feathers and has fins. Which leaf does it reach, and how many questions did you ask? Then describe what would have to change in the tree for a bird that cannot fly and has no feathers to be classified correctly.
Hints
- Start at the root question about feathers and follow the no branch.
- The no branch of the feather question leads to the fin question.
- A bird without feathers is not an animal the example covers, so consider what new question or leaf would be needed.
A tree on some dataset reports training accuracy 1.000 and test accuracy 0.80. Name two strategies that could reduce the gap, and state which of them acts during growth and which acts after growth.
Hints
- Recall the two strategies named in the overfitting section.
- One stops growth early and one removes nodes after the full tree is built.
Key Takeaways
- A decision tree is a hierarchy of if/else questions: each node tests a feature, each edge carries an answer, and each leaf holds a decision.
- The algorithm searches for the most informative test in each region, splits the data, and repeats until every leaf is pure.
- A fully grown tree overfits, which shows up as perfect training accuracy paired with lower test accuracy; pre-pruning stops growth early and post-pruning cuts back a full tree.
- Limiting
max_depthlowered training accuracy and raised test accuracy in the source example, illustrating the effect of pre-pruning. - Feature importance ranks features from 0 to 1 and sums to 1, but it does not reveal the direction of a feature's effect.