Unit 7, lesson 48

Classification Trees

How a classification tree splits feature space with yes or no questions, how Gini impurity, entropy, information gain and weighted child impurity rank splits, and what a leaf probability estimates.

  • Video 48
  • Decision Trees

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 2.52.5, 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 1,…,K1, \dots, K from a feature vector x\mathbf{x} with features x1,…,xpx_1, \dots, x_p. It is a rooted binary tree. Each internal node holds a test on one feature, on this page of the form xj≤tx_j \le t for a numerical feature xjx_j and a threshold tt, 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 R1,…,RMR_1, \dots, R_M 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 xx, known before the parcel is opened, and a label yy that is 11 when the inspection found a problem and 00 otherwise.

Score xx112233445566
Label yy000011001111

The tree with the single test x≤2.5x \le 2.5 has two leaves. The left leaf receives the parcels with scores 11 and 22, both labeled 00, and the right leaf receives the four parcels with scores 33 to 66, three of them labeled 11. Its regions are R1=(−∞,2.5]R_1 = (-\infty, 2.5] and R2=(2.5,∞)R_2 = (2.5, \infty), with estimated problem probabilities 00 and 3/43/4. With a decision threshold of 0.50.5 on that probability, the tree predicts class 00 in R1R_1 and class 11 in R2R_2.

Gini impurity

Definition Consider a node reached by nm≥1n_m \ge 1 training rows, of which nmkn_{mk} belong to class kk, and let pk=nmk/nmp_k = n_{mk}/n_m be the class proportions, with every row counted once. The Gini impurity of the node is

G=1−∑k=1Kpk2=∑k=1Kpk(1−pk).\begin{aligned} G &= 1 - \sum_{k=1}^{K} p_k^2 \\[0.4em] &= \sum_{k=1}^{K} p_k (1 - p_k) . \end{aligned}

For two classes with positive proportion pp it reduces to G=2p(1−p)G = 2p(1 - p). It is 00 exactly when the node is pure, meaning all its rows share one class, and it is largest, with value 1−1/K1 - 1/K, exactly when every class has proportion 1/K1/K, which gives 1/21/2 for two classes. If two labels are drawn independently and with replacement from the node’s rows, the probability that they differ is GG. 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 p=3/6=1/2p = 3/6 = 1/2, so its Gini impurity is 2⋅12⋅12=122 \cdot \tfrac12 \cdot \tfrac12 = \tfrac12, the largest value for two classes. The right leaf of the split at 2.52.5 has labels 1,0,1,11, 0, 1, 1, so p=3/4p = 3/4 and G=2⋅34⋅14=38G = 2 \cdot \tfrac34 \cdot \tfrac14 = \tfrac38. The left leaf is pure and has G=0G = 0. A node with three classes in proportions 12\tfrac12, 14\tfrac14 and 14\tfrac14 has G=1−14−116−116=58G = 1 - \tfrac14 - \tfrac1{16} - \tfrac1{16} = \tfrac58, below the three-class maximum 23\tfrac23.

Entropy and information gain

Definition For a node with at least one row and class proportions p1,…,pKp_1, \dots, p_K, the entropy is

H=−∑k=1Kpklog⁡2pk,H = -\sum_{k=1}^{K} p_k \log_2 p_k ,

with the convention 0log⁡20=00 \log_2 0 = 0, the limit of qlog⁡2qq \log_2 q as q→0+q \to 0^{+}. With base-2 logarithms it is measured in bits. It is 00 exactly when the node is pure, and it is largest, with value log⁡2K\log_2 K, exactly when every class has proportion 1/K1/K, so two balanced classes give 11 bit. Suppose a split sends the nn rows of a node to children with n1,…,nJn_1, \dots, n_J rows, each at least 11. The information gain of the split is the entropy of the parent minus the size-weighted entropy of the children,

IG⁡=H(parent)−∑j=1Jnjn H(child j).\begin{aligned} \operatorname{IG} &= H(\text{parent}) \\[0.4em] &\quad - \sum_{j=1}^{J} \frac{n_j}{n}\, H(\text{child } j) . \end{aligned}

The children’s class proportions, weighted by nj/nn_j/n, 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 00 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 11 bit. After the split at 2.52.5, the right leaf with p=3/4p = 3/4 has entropy

−34log⁡234−14log⁡214≈0.811 bits,\begin{aligned} &-\tfrac34 \log_2 \tfrac34 - \tfrac14 \log_2 \tfrac14 \\[0.4em] &\qquad \approx 0.811 \text{ bits}, \end{aligned}

and the pure left leaf has entropy 00. The gain is 1−46⋅0.811≈0.4591 - \tfrac46 \cdot 0.811 \approx 0.459 bits. The split at 3.53.5 leaves the labels 0,0,10, 0, 1 and 0,1,10, 1, 1, each with entropy about 0.9180.918 bits, so its gain is only about 0.0820.082 bits. The two criteria do not always agree. Take a node with 22 positive and 66 negative rows. Split A sets one positive row apart and leaves 77 rows with 11 positive, while split B makes a pure group of 44 negatives and a group of 22 positives and 22 negatives. The Gini decreases are 9/56≈0.1619/56 \approx 0.161 for A and 1/8=0.1251/8 = 0.125 for B, while the information gains are about 0.2940.294 bits for A and 0.3110.311 bits for B. Gini impurity prefers A and entropy prefers B.

Weighted child impurity

Definition Let II be an impurity function such as Gini impurity or entropy, and let a candidate split send the nn training rows of a node to a left child with nLn_L rows and a right child with nRn_R rows, where nL+nR=nn_L + n_R = n. The split is valid only if nL≥1n_L \ge 1 and nR≥1n_R \ge 1. 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

Isplit=nLn I(L)+nRn I(R),\begin{aligned} I_{\text{split}} &= \frac{n_L}{n}\, I(L) \\[0.4em] &\quad + \frac{n_R}{n}\, I(R) , \end{aligned}

and the impurity decrease is ΔI=I(parent)−Isplit\Delta I = I(\text{parent}) - I_{\text{split}}. 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 2.52.5 of the parcel data the children have 22 and 44 rows, so with Gini impurity

Isplit=26⋅0+46⋅38=14,\begin{aligned} I_{\text{split}} &= \tfrac26 \cdot 0 + \tfrac46 \cdot \tfrac38 \\[0.4em] &= \tfrac14 , \end{aligned}

and the decrease is 12−14=14\tfrac12 - \tfrac14 = \tfrac14. The split at 3.53.5 has two children of 33 rows, each with Gini impurity 2⋅13⋅23=492 \cdot \tfrac13 \cdot \tfrac23 = \tfrac49, so the weighted child impurity is 49\tfrac49 and the decrease is 12−49=118≈0.056\tfrac12 - \tfrac49 = \tfrac1{18} \approx 0.056. Between these two candidates the Gini criterion picks 2.52.5, 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 xjx_j, sort the distinct values of xjx_j 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 1.51.5, 2.52.5, 3.53.5, 4.54.5 and 5.55.5, with Gini decreases 110\tfrac1{10}, 14\tfrac14, 118\tfrac1{18}, 14\tfrac14 and 110\tfrac1{10}. The thresholds 2.52.5 and 4.54.5 tie, and the tie rule that takes the smaller threshold picks 2.52.5. 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 11 exactly when the features differ, so (0,0)(0, 0) and (1,1)(1, 1) have label 00 while (0,1)(0, 1) and (1,0)(1, 0) have label 11. Either single split leaves one point of each class in each child, so its decrease is 00 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 ℓ\ell is reached by nℓ≥1n_\ell \ge 1 training rows, nℓkn_{\ell k} of them in class kk. Counting every row once, the leaf probability of class kk is

P^(Y=k∣x∈Rℓ)=nℓknℓ,\hat{P}(Y = k \mid \mathbf{x} \in R_\ell) = \frac{n_{\ell k}}{n_\ell} ,

and the tree gives this same estimate to every input in the region RℓR_\ell. 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 P^(Y=1∣x)\hat{P}(Y = 1 \mid \mathbf{x}) 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 00 or 11, which is usually overconfident on new data. Smoothing changes the estimate, for example the Laplace rule (nℓk+1)/(nℓ+K)(n_{\ell k} + 1)/(n_\ell + K), 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 11. 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 2.52.5 gives the left leaf probability 0/2=00/2 = 0 and the right leaf probability 3/4=0.753/4 = 0.75. Under the rule that x≤tx \le t goes left, a parcel with score exactly 2.52.5 gets 00 and one with score 2.50012.5001 gets 0.750.75. If greedy splitting continues until every leaf is pure, the right node with labels 1,0,1,11, 0, 1, 1 splits at 4.54.5, whose Gini decrease 18\tfrac18 beats the 124\tfrac1{24} of both 3.53.5 and 5.55.5, and then its left part splits at 3.53.5. The final tree has the four leaves x≤2.5x \le 2.5, 2.5<x≤3.52.5 < x \le 3.5, 3.5<x≤4.53.5 < x \le 4.5 and x>4.5x > 4.5, and it makes no errors on the training rows. Its leaf for 2.5<x≤3.52.5 < x \le 3.5 holds a single parcel, so a new parcel with score 3.43.4 receives probability 11 on the evidence of one example. The Laplace rule would give (1+1)/(1+2)=2/3(1 + 1)/(1 + 2) = 2/3 instead.

Check yourself

  1. A leaf holds 88 training rows, 66 negative and 22 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 0.50.5?
Show the answer

The positive proportion is p=2/8=0.25p = 2/8 = 0.25, and that is the leaf probability. The Gini impurity is 2⋅0.25⋅0.75=0.3752 \cdot 0.25 \cdot 0.75 = 0.375, and the entropy in bits is

H=0.25log⁡24+0.75log⁡243≈0.5+0.311=0.811.\begin{aligned} H &= 0.25 \log_2 4 + 0.75 \log_2 \tfrac43 \\[0.4em] &\approx 0.5 + 0.311 = 0.811 . \end{aligned}

Since 0.250.25 is below 0.50.5, 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 0.750.75, because both measures stay the same when the two classes swap roles.

  1. A node holds 1010 rows, 55 positive and 55 negative. Split A sends 11 positive row to the left and the other 99 rows, 44 positive and 55 negative, to the right. Split B sends 44 positive and 11 negative to the left and 11 positive and 44 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 12\tfrac12. In split A the pure child has impurity 00 and the large child has p=4/9p = 4/9, so its impurity is 2⋅49⋅59=40812 \cdot \tfrac49 \cdot \tfrac59 = \tfrac{40}{81}. The weighted child impurity is

110⋅0+910⋅4081=49,\tfrac1{10} \cdot 0 + \tfrac9{10} \cdot \tfrac{40}{81} = \tfrac49 ,

a decrease of 12−49=118≈0.056\tfrac12 - \tfrac49 = \tfrac1{18} \approx 0.056. In split B both children have p=4/5p = 4/5 or p=1/5p = 1/5, so each has impurity 2⋅45⋅15=8252 \cdot \tfrac45 \cdot \tfrac15 = \tfrac8{25}, the weighted child impurity is 825\tfrac8{25} and the decrease is 12−825=950=0.18\tfrac12 - \tfrac8{25} = \tfrac9{50} = 0.18. The Gini criterion chooses B.

The unweighted average for A is 12(0+4081)=2081≈0.247\tfrac12 (0 + \tfrac{40}{81}) = \tfrac{20}{81} \approx 0.247, an apparent decrease of about 0.2530.253, 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 0.1080.108 bits for A against 0.2780.278 bits for B.

  1. A tree fitted to past parcels has a root split on the declared weight. Of 6060 training parcels with declared weight at most 22 kilograms, 33 had a problem, and of 4040 heavier ones, 1616 had a problem. A colleague concludes that asking senders to declare lower weights would reduce problems, and that exactly 4040 percent of heavier parcels will have a problem from now on. What does the tree actually support?
Show the answer

The leaf probabilities are 3/60=0.053/60 = 0.05 and 16/40=0.4016/40 = 0.40. 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 0.400.40 is an estimate from 4040 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 0.4⋅0.6/40≈0.08\sqrt{0.4 \cdot 0.6 / 40} \approx 0.08 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.

How this lesson connects