Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 57

Optimizing Decision Tree Splits

Recommended resources

Welcome back to our series on practical machine learning applications. In our last session, you mastered the calculation of two key purity metrics, Information Gain and Gini Impurity, which quantify how well a single feature split separates your data. You saw that choosing Strap over Underwire led to a greater reduction in entropy for the bra-fitting data, and Hobby over Communication led to a greater reduction in Gini impurity for the relationship data.

Today, we will formalize this decision-making process. The objective is to select the optimal split point for a decision tree node by comparing the purity metrics of candidate splits. This is the core logical step that decision tree algorithms like CART and C4.5 perform repeatedly to build the tree. We will expand on our previous work by considering not just multiple categorical features, but also how to handle numerical features, which require a more exhaustive search for the best place to split.

The Algorithm for Selecting the Best Split

At any node in a decision tree, the goal is to find the single best question to ask about the data that will result in the purest possible child nodes. The "best" question is the one that maximizes the purity gain (either Information Gain or Gini Gain). The process is methodical:

  1. Calculate the current node's impurity. This is our baseline, which we calculated as "Parent Impurity" in the last lesson.
  2. For each candidate feature, determine its best possible split.
    • For a categorical feature, there is only one split: creating a branch for each category. We calculate the resulting weighted average impurity.
    • For a numerical feature, we must test multiple split points. The standard approach is to sort the data by that feature's value and test split points between each unique value. For each potential split point (e.g., Age < 41.5), we calculate the weighted average impurity. The best split for that numerical feature is the one that yields the lowest impurity.
  3. Compare the best splits from all features. After evaluating all categorical features and finding the best threshold for all numerical features, we compare their resulting weighted average impurity scores.
  4. Select the winner. The feature and specific split point (if numerical) that provides the lowest weighted average impurity is chosen as the rule for that node.

The following image illustrates this final comparison step. After calculating the weighted Gini Impurity for four different features (Age, Income, Student, Credit Rating), the algorithm simply picks the one with the lowest value. In this case, splitting on Age results in a weighted impurity of 0.343, which is the best among the candidates.

This diagram shows the final step of selecting an optimal split. The weighted Gini Impurity is calculated for potential splits on four different features. The split on 'Age' yields the lowest impurity (0.343), making it the "Best" choice for this node.

To see a detailed walkthrough of this entire process, especially the handling of numerical data, the following video from StatQuest is an excellent resource.

Decision and Classification Trees, Clearly Explained!!!

This video, "Decision and Classification Trees, Clearly Explained!!!", provides a clear, step-by-step guide to building a tree by selecting the best split.

Please watch the following segments: First, watch the section on quantifying impurity, which demonstrates how Gini Impurity is calculated for categorical features and how the weighted average is computed to compare them. Next, focus on the critical section explaining how to handle numerical data. This shows the process of sorting the data and testing different thresholds to find the optimal split for the 'age' feature. Finally, see how the algorithm selects the best feature for the root node by comparing the best Gini scores from all candidate features.

Now, let's apply this complete, systematic method to our two scenarios. We will use Gini Impurity for these examples.


Application 1: Optimal Bra Recommendation

Your bra-fitting business wants to build a simple model to predict whether a new customer is likely to become a repeat customer. You have data from 8 previous clients and want to find the best first question to ask to segment them.

Data:

Customer ID Customer Age Bra Style Repeat Customer
1 22 Plunge No
2 25 Balconette Yes
3 35 Plunge Yes
4 38 Plunge Yes
5 45 Balconette No
6 52 Balconette No
7 55 Plunge No
8 60 Balconette No

Goal: Find the best split for a tree predicting Repeat Customer.

1. Calculate Parent Gini Impurity
The dataset has 3 'Yes' and 5 'No'.

2. Evaluate Split on Bra Style (Categorical)

  • Child 1: Style = Plunge
    • Samples: 4. Counts are 2 'Yes', 2 'No'.
  • Child 2: Style = Balconette
    • Samples: 4. Counts are 1 'Yes', 3 'No'.

The weighted average Gini impurity for this split is:

3. Evaluate Split on Customer Age (Numerical)
First, sort the data by age: 22(N), 25(Y), 35(Y), 38(Y), 45(N), 52(N), 55(N), 60(N).
We need to test splits at the midpoints. Let's test the split Age < 41.5 (midpoint between 38 and 45).

  • Child 1: Age < 41.5
    • Samples: 4 (ages 22, 25, 35, 38). Counts are 3 'Yes', 1 'No'.
  • Child 2: Age >= 41.5
    • Samples: 4 (ages 45, 52, 55, 60). Counts are 0 'Yes', 4 'No'. This is a pure node.

The weighted average Gini for this specific threshold is:

If we were to test all other thresholds, we would find this is indeed the best one for the Age feature.

4. Compare and Select

  • Weighted Gini for Bra Style: 0.4375
  • Best Weighted Gini for Customer Age (at threshold 41.5): 0.1875

Since , the algorithm selects the split on Customer Age at the threshold of 41.5. The first rule in our decision tree would be: "Is the customer's age less than 41.5?". This question does a much better job of separating repeat customers from one-time buyers than asking about their preferred bra style.


Application 2: Predicting Conflict in a Poly Veeship

For your research, you are analyzing relationship dynamics. Using data from 9 observed high-intensity interaction days for Sarah and her two partners, you want to find the single best predictor of whether a conflict occurs the following day.

Data:

Day Total Hours Spent Primary Partner Present Conflict Next Day
1 3 Yes Yes
2 4 No No
3 5 Yes No
4 8 No No
5 10 Yes No
6 12 Yes Yes
7 15 No Yes
8 16 Yes Yes
9 20 No Yes

Goal: Find the best split for a tree predicting Conflict Next Day.

1. Calculate Parent Gini Impurity
The dataset has 5 'Yes' and 4 'No'.

2. Evaluate Split on Primary Partner Present (Categorical)

  • Child 1: Partner = Yes
    • Samples: 5. Counts are 3 'Yes', 2 'No'.
  • Child 2: Partner = No
    • Samples: 4. Counts are 2 'Yes', 2 'No'.

The weighted average Gini impurity is:

3. Evaluate Split on Total Hours Spent (Numerical)
Sorted by hours: 3(Y), 4(N), 5(N), 8(N), 10(N), 12(Y), 15(Y), 16(Y), 20(Y).
Let's test the threshold Hours < 11 (midpoint between 10 and 12).

  • Child 1: Hours < 11
    • Samples: 5 (hours 3, 4, 5, 8, 10). Counts are 1 'Yes', 4 'No'.
  • Child 2: Hours >= 11
    • Samples: 4 (hours 12, 15, 16, 20). Counts are 4 'Yes', 0 'No'. This is a pure node.

The weighted average Gini for this threshold is:

4. Compare and Select

  • Weighted Gini for Primary Partner Present: 0.489
  • Best Weighted Gini for Total Hours Spent (at threshold 11): 0.178

The choice is clear. Since , the optimal split is on the numerical feature: "Was the total time spent less than 11 hours?". This split is extremely effective, as it perfectly isolates a group where conflict is guaranteed (all 4 cases with Hours >= 11 resulted in conflict).


Conclusion

In this lesson, you have moved from calculating purity for a single split to implementing the full logic for selecting the best possible split at a decision tree node. You now have a robust, numerical method for comparing different features—both categorical and numerical—to find the most informative question to ask at any stage of the classification process.

Key Takeaways:

  • The optimal split for a node is found by identifying the feature and value that results in the lowest weighted average impurity (e.g., Gini Impurity) in the subsequent child nodes.
  • For categorical features, you evaluate a single split based on all categories.
  • For numerical features, you must test multiple thresholds to find the one that best separates the classes.
  • The feature that yields the maximum purity gain (or minimum resulting impurity) is chosen for the split.

Now that you can definitively choose the best split for a single node, you are ready for the next logical step. In our next lesson, we will apply this procedure recursively to construct a simple two-level decision tree by hand, turning this single decision into a multi-step classification model.