Skip to main content

Welcome to a Grasp lesson

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

Lesson 56

Calculating Information Gain and Gini Impurity

Welcome back. In our previous lessons, we've explored classification using distance-based methods (K-Nearest Neighbors) and probabilistic models (Naive Bayes). Naive Bayes, in particular, builds a model by learning the likelihood of features given a certain class. Today, we shift our focus to a different and highly intuitive approach you've identified as "so useful in decision making": Decision Trees.

Instead of probabilities or distances, decision trees create a model that is essentially a set of hierarchical rules, much like a flowchart. The core challenge in building such a tree is deciding which rule to apply at each step. This lesson will equip you with the mathematical tools for making that decision. The learning outcome is to calculate the Information Gain and Gini Impurity for a potential split for a given dataset and feature. These two metrics are the standard ways to quantify which feature provides the "best" split.

Purity, Entropy, and Information Gain

The goal of a decision tree is to split the data into groups that are as "pure" as possible, meaning each group ideally contains data points of only a single class. A perfect split would separate all "Yes" cases from all "No" cases. To measure how close we are to this ideal, we need a metric for impurity.

As a statistician, you can think of impurity as a measure of uncertainty or disorder in a set of data. A dataset with a 50/50 mix of two classes has maximum uncertainty, while a dataset with 100% of one class has zero uncertainty. In information theory, this concept is formalized as Entropy.

For a given dataset with classes, the entropy is calculated as:

where is the proportion of samples in that belong to class . The logarithm is base 2, so the unit of entropy is "bits".

  • If a set is pure ( for some class ), the entropy is 0.
  • For a two-class problem, entropy is maximized at 1 bit when the classes are evenly split (50/50).

When we split a dataset (a parent node) into subsets (child nodes) using a feature, we want the resulting child nodes to be purer, on average, than the parent. The Information Gain measures this reduction in entropy. It is the metric used by algorithms like ID3 and C4.5.

The formula is:

This is the entropy of the parent node minus the weighted average of the entropies of the child nodes. The algorithm chooses the split with the highest information gain.

The image below illustrates the calculation for a single split. We will now perform this calculation for our scenarios.

This diagram shows the core logic of Information Gain. The entropy of the parent node (E parent) is calculated, and from it, the weighted average entropy of the two child nodes is subtracted to find the gain from splitting on attribute X.


Application 1: Predicting Bra Fit Complaints with Information Gain

Let's use the same training data from our Naive Bayes lesson to decide the best initial split for a decision tree. Our goal is to predict Complaint (Yes/No) based on Underwire and Strap type.

  • Target: Complaint (5 'Yes', 9 'No')
  • Total Samples: 14
  • Features for splitting: Underwire (Metal, Plastic), Strap (Wide, Thin)

1. Calculate Parent Entropy

First, we calculate the entropy of the entire dataset before any split.


2. Evaluate Split on Underwire

  • Child 1: Underwire = Metal
    • Samples: 8 total. Counts are 4 'Yes' and 4 'No'.
    • ,
  • Child 2: Underwire = Plastic
    • Samples: 6 total. Counts are 1 'Yes' and 5 'No'.
    • ,

Now, calculate the Information Gain for this split:

3. Evaluate Split on Strap

  • Child 1: Strap = Wide
    • Samples: 7 total. Counts are 1 'Yes' and 6 'No'.
  • Child 2: Strap = Thin
    • Samples: 7 total. Counts are 4 'Yes' and 3 'No'.

Calculate the Information Gain for this split:

Conclusion: Since , the decision tree algorithm would choose to split on the Strap feature first, as it does a better job of separating clients who complain from those who don't.



Gini Impurity

An alternative to Information Gain is the Gini Impurity, used by the CART (Classification and Regression Trees) algorithm. Gini Impurity measures the probability of incorrectly classifying a randomly chosen element in the dataset if it were randomly labeled according to the class distribution in the set.

The formula for a dataset is:

Like entropy, Gini Impurity is 0 for a pure set and maximized for a mixed set (at 0.5 for a two-class problem). It is computationally less expensive as it avoids the calculation.

The decision rule is to choose the split that maximizes the Gini Gain, which is the reduction in impurity:

This process builds a tree like the one shown below, where each node displays its Gini impurity value.

An example of a complete decision tree for the Iris dataset. Note how each node includes its Gini impurity score, the number of samples, and the class distribution. The tree is built by repeatedly choosing splits that minimize the Gini impurity in the resulting child nodes.

Application 2: Predicting Relationship Conflict with Gini Impurity

Now, let's use Gini Impurity to find the best first split for Sarah's relationship data. Our goal is to predict Conflict (Yes/No).

  • Target: Conflict (5 'Yes', 10 'No')
  • Total Samples: 15
  • Features for splitting: Communication (Direct, Indirect), Hobby (Shared, Separate)

1. Calculate Parent Gini Impurity

2. Evaluate Split on Communication

  • Child 1: Communication = Direct
    • Samples: 9 total. Counts are 2 'Yes' and 7 'No'.
  • Child 2: Communication = Indirect
    • Samples: 6 total. Counts are 3 'Yes' and 3 'No'.

Now, calculate the Gini Gain for this split:

3. Evaluate Split on Hobby

  • Child 1: Hobby = Shared
    • Samples: 10 total. Counts are 1 'Yes' and 9 'No'.
  • Child 2: Hobby = Separate
    • Samples: 5 total. Counts are 5 'Yes' and 0 'No'.
    • This is a pure node.

Calculate the Gini Gain for this split:

Conclusion: The Gini Gain for splitting on Hobby (0.324) is much greater than for Communication (0.036). Therefore, the CART algorithm would choose Hobby as the first splitting feature. This makes intuitive sense: splitting on Hobby creates a perfectly pure node (Hobby=Separate -> Conflict=Yes), which is the ideal outcome of a split.


Conclusion

In this lesson, you've mastered the calculations at the heart of building a decision tree. You now understand how to quantify the quality of a potential split using two industry-standard metrics. This is the fundamental building block for creating the rule-based models you find so useful.

Key Takeaways:

  • Decision trees are built by finding splits that increase the "purity" of the resulting data subsets.
  • Entropy is a measure of impurity or uncertainty, with 0 representing a perfectly pure set.
  • Information Gain measures the reduction in entropy achieved by a split. The split with the highest gain is chosen.
  • Gini Impurity is an alternative, computationally faster measure of impurity.
  • Gini Gain measures the reduction in Gini Impurity. The split with the highest Gini Gain is chosen.

You have now calculated these values by hand for both of your key scenarios. In our next lesson, we will use this skill to move from evaluating a single split to constructing a complete, albeit simple, two-level decision tree.