← All algorithms

Classification

Decision Tree

2023-12-19

The decision tree algorithm is a prominent supervised learning method for classification. It calculates the information gain for all possible conditions at branching points and prunes the tree by maximizing this gain. This process is repeated at each subsequent branching point until all nodes correspond to the same class, which leads to termination.

The concept of information gain is defined as the difference in impurity between parent and child nodes.

Information Gain (IG) = Impurity of the parent node (Ip) - Weighted average of the impurities of the child nodes (Ij)

\(IG=I_p-\sum _j^{\ }I_jN_j/N_p\)

Here, Np is the total number of samples in the parent node, and equal to the sum of samples in the child nodes.

\(N_p=\sum _j^{\ }N_j\)

The indicators commonly used to measure impurity include the Gini index, entropy, and classification error. The definitions of each are as follows.

Gini Index = \(\sum _j^{\ }N_j/N_p(1-N_j/N_p)=1-\sum _j^{\ }(N_j/N_p)^2\)

Information Entropy = \(\sum _j^{\ }N_j/N_p\log _2(N_j/N_p)\)

Classification Error = \(1-\max _{\ }^{ }(N_j/N_p)\)

In general, regardless of which measure of impurity you use, you will get similar results. However, when it comes to Excel functions, for Gini Index, we only need to be cautious when Np is 0 (which causes an error due to divide by zero). But for Entropy, you also need to consider the case when Nj is 0 (which causes an extra error due to log (0). Therefore, it is more robust to use Gini Index than Entropy.

For the implementing the Decision Tree in Excel, we use the Iris dataset using Gini index.

Iris flowers dataset uses 4 Source parameters to classify 3 types of iris flowers. With 50 samples for each class and a total of 150 samples, the Gini index is calculated to be 0.66.

CountPGini Index
Setosa500.330.22
Versicolor500.330.22
Virginica500.330.22
Sum1500.66

​Gini Index = \(1 - (0.33^2+0.33^2+0.33^2) = 0.22+0.22+0.22=0.66\)​

​

Among the 4 Source parameters (Sepal Length/Width, Petal Length/Width), we need to find which condition, based on the Gini Index, generates the highest information gain. By using a unit of 0.05 for the Source parameters, the following picture shows the IG distribution observed based on the value of the Source parameters.

​

​

From the plot, the Source parameters and their values for branching to create the largest IG are Petal Width = 0.775 or Petal Length = 2.45, which resulting in IG = 0.33.

To simplify the problem and perform classification using only Petal Length and Width, let's train a decision tree up to a depth of 4.

Step 1. Take 100 samples from the entire dataset as a training set, and reserve the remaining 50 as the test set. Calculate the Gini Index.

CountPGini Index
Setosa340.340.2234
Versicolor330.330.2211
Virginica330.330.2211
Sum1000.6666

Step 2. For the two source parameters, Petal Length and Petal Width, count the number of samples greater than and the ones for less than each value, starting from the minimum value to the maximum value, with an increment of 0.05. Then, calculate the sample ratio and calculate the Gini index.

For each conditional statement, we will have one count to be true, and one count to be false, therefore there are total 6 counts for all three kinds of classes. In the excel worksheet excerpt below, the red color represents the case when the condition if(Petal Length <= 1.05) is true, while the blue color represents the case when it is false. You can use Excel's COUNTIFS function to count the number of samples in each category.

=COUNTIFS($A$2:$A$101,F$9,$B$2:$B$101,"<="&$E10)

<Note> COUNTIFS(target1,condition1, target2,condition2, ...) reports the number of cells to meet all the conditions to the targets.

In the given case, when the Petal Length is less than 1.5, the sample count is 24/0/0 and when it is greater, the sample count is 10/33/33, which results in the Gini index as below.

\(\sum _j^{\ }I_jN_j/N_p=(24/100)*(1-(24/24)^2)+(76/100)*(1-(10/76)^2-(33/76)^2-(33/76)^2)=0.46\)

Then subtracting the impurity of the child nodes from the impurity of the parent node, the information gain becomes

\(IG=I_p-\sum _j^{\ }I_jN_j/N_p=0.666-0.46=0.206\)

By iterating this process for all ranges of Petal Length and Petal Width, we can obtain the IG Plot for the samples, and the point that maximizes IG becomes the condition for the first split.

​

The condition for the first branch is set at Petal Length = 2.575 where the IG becomes the maximum. (Though there are two identical maximum conditions, here we arbitrary choose the Petal Length over the Petal Width)

Step 3. For Petal Length <= 2.575, the IG is 0. For Petal Length > 2.575, the IG is 0.5. We will continue with Petal Length > 2.575 to find child branches.

As Petal Length > 2.575 is used to calculate the Information Gain (IG) over the range of Petal Width and Petal Length, which is shown in the plot below, we can determine the splitting point using the position of the maximum point. In this case, the maximum IG value occurs at Petal Width = 1.575.

​

Step 4. Calculate the IG distribution for all source parameters for the following four cases, using Petal Length=2.575 and Petal Width=1.575.

Case 1: Petal Length <= 2.575, Petal Width <=1.575

Case 2: Petal Length <= 2.575, Petal Width > 1.575

Case 3: Petal Length > 2.575, Petal Width <=1.575

Case 4: Petal Length > 2.575, Petal Width > 1.575

The impurity in the parent node obtained for the four cases below is as follows.

Patal LengthPetal WidthIG
Case 1<=2.575<=1.5750
Case 2<=2.575>1.5750
Case 3>2.575<=1.5750.06243
Case 4>2.575>1.5750.15673

​

Now, for each case, the IG for each parent node is calculated as shown in the figure below. If a case is not depicted in the figure, its IG value is considered to be 0. Similarly, by finding the position of the maximum IG value, we can determine that Petal Length=4.875 is the next branch point.

​

You can create conditional statements for the following 8 cases.

Case 1: Petal Length <= 2.575, Petal Width <=1.575, Petal Length <=4.875

Case 2: Petal Length <= 2.575, Petal Width <=1.575, Petal Length >4.875

Case 3: Petal Length <= 2.575, Petal Width > 1.575, Petal Length <=4.875

Case 4: Petal Length <= 2.575, Petal Width > 1.575, Petal Length >4.875

Case 5: Petal Length > 2.575, Petal Width <=1.575, Petal Length <=4.875

Case 6: Petal Length > 2.575, Petal Width <=1.575, Petal Length >4.875

Case 7: Petal Length > 2.575, Petal Width > 1.575, Petal Length <=4.875

Case 8: Petal Length > 2.575, Petal Width > 1.575, Petal Length >4.875

Step 5. Once again, calculate the impurity for each of the 8 cases related to the parents. Their results are summarized at the table below:

CasePetal LengthPetal WidthPetal LengthIG
1<=2.575<=1.575<=4.8750
2<=2.575<=1.575>4.8750
3<=2.575>1.575<=4.8750
4<=2.575>1.575>4.8750
5>2.575<=1.575<=4.8750
6>2.575<=1.575>4.8750.444
7>2.575>1.575<=4.8750.5
8>2.575>1.575>4.8750.06243

From this table, the maximum IG is for the case 7 and we calculate the Information Gain (IG) for the entire range of the source parameters with 2.575< Patel Length < 4.875, Petal Width>1.575. The IG vs. Source parameter plot is shown below. The maximum IG is obtained when Petal Length=2.875.

​

Step 6. The number of condition statements that can be created with the 4 possible cases of Petal Length = 2.575, Petal Width = 1.575, Petal Length = 4.875, and Petal Length = 2.875 are 16. The number of instances satisfying each class for each condition statement are as follows. Here, Petal Length and Petal Width are encoded as index 1 and 2 respectively, and the classes of Decision are encoded as 1, 2, and 3.

​

​

For example, if arbitrary data satisfies the conditions Patal Length <=2.575, Petal Width <= 1.575, Petal Length <=4.875, Petal Length <=2.875, it is classified as Iris-Setosa.

If the conditions of "Patal Length >2.575, Petal Width > 1.575, Petal Length >4.875, Petal Length >2.875" are satisfied, it is classified as Iris-Virginica.

Step 7. When evaluating the model with test data using the decision boundaries from the rule provided in the above table, the results are as follows.

​