A parcel inspection team wants a rule that says which parcels to open, and it wants to read that rule, not only run it. A classification tree gives such a rule as a short chain of questions, such as whether a parcel’s score is at most , where each answer leads either to another question or to a final estimate. Which question should come first, and how far can the estimate at the end be trusted? This lesson measures how mixed a group of labels is with Gini impurity and entropy, chooses each split by the size-weighted drop in that mixture, and reads what the resulting leaf probabilities do and do not say.
Key terms
Classification tree
Definition A classification tree predicts a categorical target with classes from a feature vector with features . It is a rooted binary tree. Each internal node holds a test on one feature, on this page of the form for a numerical feature and a threshold , and sends an input to its left child when the test holds and to its right child otherwise. Each leaf holds a predicted class, usually together with estimated class probabilities. Following the tests from the root, every input reaches exactly one leaf, so the leaves divide feature space into disjoint regions that together cover it, and the tree gives the same prediction to all inputs in one region. The tests may use only features that are available when the prediction is made. Decision tree is the general name, and a classification tree is a decision tree for a categorical target.
In plain words A classification tree is a flowchart of yes or no questions about the features. Each path from the top to a final box reads as a rule, and the final boxes cut feature space into box-shaped pieces whose sides are parallel to the axes. A rule describes what the fitted tree predicts for inputs in its piece. It does not say what causes the outcome.
Example The constructed data on this page describe six parcels. Each has a dimensionless inspection score , known before the parcel is opened, and a label that is when the inspection found a problem and otherwise.
| Score | ||||||
|---|---|---|---|---|---|---|
| Label |
The tree with the single test has two leaves. The left leaf receives the parcels with scores and , both labeled , and the right leaf receives the four parcels with scores to , three of them labeled . Its regions are and , with estimated problem probabilities and . With a decision threshold of on that probability, the tree predicts class in and class in .
Gini impurity
Definition Consider a node reached by training rows, of which belong to class , and let be the class proportions, with every row counted once. The Gini impurity of the node is
For two classes with positive proportion it reduces to . It is exactly when the node is pure, meaning all its rows share one class, and it is largest, with value , exactly when every class has proportion , which gives for two classes. If two labels are drawn independently and with replacement from the node’s rows, the probability that they differ is . The proportions need at least one row, so an empty node has no Gini impurity. Gini impurity describes how mixed the training labels are, and it says nothing about what a false positive or a false negative costs.
In plain words Gini impurity scores how mixed a group of labels is. A group with a single class scores zero, and an even mix of the classes scores the most. It is the chance that two rows picked at random from the group, with replacement, belong to different classes.
Example The root of the parcel data has positive proportion , so its Gini impurity is , the largest value for two classes. The right leaf of the split at has labels , so and . The left leaf is pure and has . A node with three classes in proportions , and has , below the three-class maximum .
Entropy and information gain
Definition For a node with at least one row and class proportions , the entropy is
with the convention , the limit of as . With base-2 logarithms it is measured in bits. It is exactly when the node is pure, and it is largest, with value , exactly when every class has proportion , so two balanced classes give bit. Suppose a split sends the rows of a node to children with rows, each at least . The information gain of the split is the entropy of the parent minus the size-weighted entropy of the children,
The children’s class proportions, weighted by , average to the parent’s, and entropy and Gini impurity are strictly concave functions of the proportions. So the information gain and the Gini decrease of the next card are never negative, and each is exactly when every child has the same class proportions as the parent. Some authors use the name information gain for a decrease in any impurity, so a reported gain should name its impurity.
In plain words Entropy is a second score for how mixed the labels are, measured in bits. Information gain says how much a split lowers that score, averaged over the rows. Gini impurity and entropy agree about what pure and evenly mixed mean, but they weigh the mixtures in between differently, so now and then they prefer different splits.
Example The root of the parcel data has entropy bit. After the split at , the right leaf with has entropy
and the pure left leaf has entropy . The gain is bits. The split at leaves the labels and , each with entropy about bits, so its gain is only about bits. The two criteria do not always agree. Take a node with positive and negative rows. Split A sets one positive row apart and leaves rows with positive, while split B makes a pure group of negatives and a group of positives and negatives. The Gini decreases are for A and for B, while the information gains are about bits for A and bits for B. Gini impurity prefers A and entropy prefers B.
Weighted child impurity
Definition Let be an impurity function such as Gini impurity or entropy, and let a candidate split send the training rows of a node to a left child with rows and a right child with rows, where . The split is valid only if and . A child with no rows has no class proportions, so its impurity is undefined, and a split that sends every row the same way does not divide the node at all. For a valid split, the weighted child impurity is
and the impurity decrease is . The weights are the children’s shares of the rows at the node. With sample weights, every count becomes a sum of weights. Some software multiplies every decrease at a node by the node’s share of all training rows, which changes the numbers but not which split wins at that node.
In plain words A split is judged by the impurity of its children counted per row. A large mixed child affects many rows, so it counts for more than a small pure child. Without the weights, a split that sets one row apart in a pure child would look far better than it is.
Example For the split at of the parcel data the children have and rows, so with Gini impurity
and the decrease is . The split at has two children of rows, each with Gini impurity , so the weighted child impurity is and the decrease is . Between these two candidates the Gini criterion picks , and so does information gain.
Greedy splitting
Definition Greedy splitting grows a tree from the root by repeating one step at each node. First collect the valid candidate splits. For a numerical feature , sort the distinct values of among the node’s rows and use the midpoints between consecutive values as thresholds, so every candidate has two nonempty children. Then choose the candidate with the largest impurity decrease, using a fixed rule to break ties, split the node, and repeat the step in each child. A node becomes a leaf when a stopping rule applies, for example when it is pure, when every feature is constant on its rows so that no valid split exists, when it reaches a maximum depth, when every split would leave a child with fewer than a minimum number of rows, or when the best decrease is below a set amount. Each choice looks only one split ahead, so greedy splitting is not guaranteed to find the best tree of a given size or depth. Searching all trees for the best one is a hard combinatorial problem in general, which is why the greedy method is the standard one.
In plain words At every node the tree takes the question that helps most right now and never revisits it. That is fast and often works well, but a question that looks useless on its own can be the one that makes the next questions work.
Example For the six parcels the candidate thresholds are , , , and , with Gini decreases , , , and . The thresholds and tie, and the tie rule that takes the smaller threshold picks . Another tie rule would start the same data with a different split. To see the limit of looking one step ahead, take four points with two binary features, labeled exactly when the features differ, so and have label while and have label . Either single split leaves one point of each class in each child, so its decrease is under Gini impurity and entropy alike. A rule that stops when no split lowers the impurity stops at the root, yet splitting each child on the other feature gives a tree of depth two that classifies all four points correctly.
Leaf probability
Definition Suppose leaf is reached by training rows, of them in class . Counting every row once, the leaf probability of class is
and the tree gives this same estimate to every input in the region . The predicted class is the most frequent class in the leaf, with a fixed rule for ties, or, with two classes, the result of comparing with a decision threshold. A leaf probability is an estimate from the training rows of one region. It can be far off when the leaf holds few rows, and leaves grown until they are pure report or , which is usually overconfident on new data. Smoothing changes the estimate, for example the Laplace rule , and weighted rows give weighted proportions, so either choice should be stated when it is used. Whether the probabilities are calibrated has to be checked on held-out data.
In plain words A leaf’s probability is the share of each class among the training rows that ended up in it. Two out of two is far weaker evidence than two hundred out of two hundred, although both give a probability of . The probability also jumps at every threshold, which is a property of the fitted rule and not evidence that the real risk jumps there.
Example The split at gives the left leaf probability and the right leaf probability . Under the rule that goes left, a parcel with score exactly gets and one with score gets . If greedy splitting continues until every leaf is pure, the right node with labels splits at , whose Gini decrease beats the of both and , and then its left part splits at . The final tree has the four leaves , , and , and it makes no errors on the training rows. Its leaf for holds a single parcel, so a new parcel with score receives probability on the evidence of one example. The Laplace rule would give instead.
Check yourself
- A leaf holds training rows, negative and positive. Find its leaf probability for the positive class, its Gini impurity and its entropy. Which class does it predict with a decision threshold of ?
Show the answer
The positive proportion is , and that is the leaf probability. The Gini impurity is , and the entropy in bits is
Since is below , the leaf predicts the negative class. These are the same impurity values as for the right leaf of the parcel split, whose positive proportion is , because both measures stay the same when the two classes swap roles.
- A node holds rows, positive and negative. Split A sends positive row to the left and the other rows, positive and negative, to the right. Split B sends positive and negative to the left and positive and negative to the right. An analyst averages the two children’s Gini impurities without weights and picks split A because it has a pure child. Which split does the Gini criterion actually choose?
Show the answer
The parent has Gini impurity . In split A the pure child has impurity and the large child has , so its impurity is . The weighted child impurity is
a decrease of . In split B both children have or , so each has impurity , the weighted child impurity is and the decrease is . The Gini criterion chooses B.
The unweighted average for A is , an apparent decrease of about , which would beat B. That is the mistake. The pure child holds one row out of ten, while the mixed child holds nine, and only the weights account for that. Information gain agrees with the weighted Gini result, about bits for A against bits for B.
- A tree fitted to past parcels has a root split on the declared weight. Of training parcels with declared weight at most kilograms, had a problem, and of heavier ones, had a problem. A colleague concludes that asking senders to declare lower weights would reduce problems, and that exactly percent of heavier parcels will have a problem from now on. What does the tree actually support?
Show the answer
The leaf probabilities are and . They describe a predictive association in the training parcels. Heavier declared weights went together with more problems, perhaps because weight is linked to the kind of goods, the sender or the route. The tree does not show that weight causes problems, and changing what senders write on a form would not change what is inside the parcels. A causal claim needs a design built for it, such as a randomized experiment, not a fitted split.
The value is an estimate from rows, not a guaranteed future rate. Even ignoring that the split was chosen because it fit these same rows well, a binomial standard error of about shows how loose it is. Before the numbers are used as probabilities for future parcels, the tree should be scored on held-out parcels from the period and population where it will be used, and its calibration checked there.