Calculate Information Gain Using MATLAB – Decision Tree Feature Selection


Calculate Information Gain Using MATLAB Concepts

Information Gain Calculator

Enter the class distribution for your initial dataset and for each split created by an attribute. This calculator supports up to 3 attribute splits (child nodes).


Number of instances belonging to the ‘Positive’ class in the original dataset.


Number of instances belonging to the ‘Negative’ class in the original dataset.

Attribute Split 1 (Child Node 1)


Number of ‘Positive’ instances in the first subset after splitting by an attribute value.


Number of ‘Negative’ instances in the first subset.

Attribute Split 2 (Child Node 2)


Number of ‘Positive’ instances in the second subset after splitting.


Number of ‘Negative’ instances in the second subset.

Attribute Split 3 (Child Node 3 – Optional)


Number of ‘Positive’ instances in the third subset (leave 0 if not applicable).


Number of ‘Negative’ instances in the third subset (leave 0 if not applicable).



What is Information Gain?

Information Gain is a crucial metric in machine learning, particularly in the construction of decision trees. It quantifies the reduction in entropy (or impurity) in a dataset after it is split on an attribute. Essentially, it measures how much “information” a feature provides about the class label. A higher Information Gain indicates a more effective attribute for classification, making it a prime candidate for splitting a node in a decision tree algorithm like ID3 or C4.5. Understanding how to calculate information gain using MATLAB or similar computational tools is fundamental for data scientists and machine learning engineers.

Who Should Use Information Gain?

Information Gain is primarily used by:

  • Data Scientists and Machine Learning Engineers: For building and optimizing decision tree models, especially in feature selection.
  • Students and Researchers: Studying classification algorithms, information theory, and data mining.
  • Anyone working with structured data: Who needs to understand the predictive power of different features for a target variable.

Common Misconceptions about Information Gain

  • It’s always the best metric: While powerful, Information Gain has a bias towards attributes with a large number of distinct values. This is because attributes with more values tend to create more, smaller, and purer subsets, artificially inflating their perceived gain. Gain Ratio is often used to mitigate this bias.
  • It’s only for binary classification: Information Gain can be applied to multi-class classification problems as well, by calculating entropy based on the distribution of all classes.
  • It’s complex to calculate: As this calculator demonstrates, the core calculation involves basic arithmetic and logarithms, making it straightforward once the concept of entropy is understood. Implementing how to calculate information gain using MATLAB involves similar mathematical functions.

Information Gain Formula and Mathematical Explanation

To calculate Information Gain, we first need to understand Entropy. Entropy, in the context of information theory, measures the impurity or randomness of a set of samples. A set with perfect purity (all samples belong to the same class) has an entropy of 0. A set with maximum impurity (samples are equally distributed among classes) has an entropy of 1 (for binary classification).

Step-by-Step Derivation:

  1. Calculate Initial Entropy (Entropy of the Parent Node):

    This is the entropy of the dataset before any split. For a binary classification problem with ‘Positive’ (P) and ‘Negative’ (N) classes:

    Entropy(S) = - (P / (P+N)) * log2(P / (P+N)) - (N / (P+N)) * log2(N / (P+N))

    Where S is the dataset, P is the count of positive instances, and N is the count of negative instances. If a probability is zero, the corresponding term 0 * log2(0) is treated as zero.

  2. Calculate Entropy for Each Child Node (After Split):

    For each possible value of an attribute (e.g., ‘Outlook’ having ‘Sunny’, ‘Overcast’, ‘Rain’), the dataset is split into subsets. For each subset (Sv), calculate its entropy using the same formula:

    Entropy(Sv) = - (Pv / (Pv+Nv)) * log2(Pv / (Pv+Nv)) - (Nv / (Pv+Nv)) * log2(Nv / (Pv+Nv))

    Where Pv and Nv are the positive and negative counts in the subset Sv.

  3. Calculate Weighted Average Entropy of the Splits:

    This is the sum of the entropies of each child node, weighted by the proportion of samples in that child node relative to the parent node:

    Weighted Average Entropy = Σv∈Values(A) (|Sv| / |S|) * Entropy(Sv)

    Where |Sv| is the number of samples in subset Sv, and |S| is the total number of samples in the parent dataset.

  4. Calculate Information Gain:

    Finally, Information Gain is the difference between the initial entropy and the weighted average entropy of the splits:

    Information Gain(S, A) = Entropy(S) - Weighted Average Entropy

    A higher Information Gain value indicates that the attribute A is more effective at reducing the impurity of the dataset, making it a better choice for a decision tree split. This is the core concept when you calculate information gain using MATLAB for feature selection.

Variables Table

Key Variables in Information Gain Calculation
Variable Meaning Unit Typical Range
S The entire dataset (parent node) Samples Any positive integer
A The attribute (feature) being evaluated for splitting N/A Categorical or numerical
P Number of positive instances in a dataset/subset Samples 0 to Total Samples
N Number of negative instances in a dataset/subset Samples 0 to Total Samples
Entropy(S) Impurity of the parent dataset Bits 0 to 1 (for binary classification)
Sv Subset of S where attribute A has value v Samples Any positive integer
Entropy(Sv) Impurity of a child node (subset) Bits 0 to 1
|Sv| / |S| Proportion of samples in a child node relative to the parent Ratio 0 to 1
Information Gain Reduction in entropy after splitting on attribute A Bits 0 to Entropy(S)

Practical Examples (Real-World Use Cases)

Let’s illustrate how to calculate information gain using MATLAB concepts with practical examples, focusing on a common scenario: deciding which feature to split on in a decision tree.

Example 1: Deciding to Play Tennis (Binary Classification)

Imagine a dataset used to predict if someone will ‘Play Tennis’ (Yes/No) based on weather conditions. We want to evaluate the ‘Outlook’ attribute, which can be ‘Sunny’, ‘Overcast’, or ‘Rain’.

Initial Dataset:

  • Total Samples: 14
  • Play Tennis = Yes (Positive): 9
  • Play Tennis = No (Negative): 5

Attribute ‘Outlook’ Splits:

  • Outlook = Sunny (Split 1):
    • Yes: 2
    • No: 3
    • Total: 5
  • Outlook = Overcast (Split 2):
    • Yes: 4
    • No: 0
    • Total: 4
  • Outlook = Rain (Split 3):
    • Yes: 3
    • No: 2
    • Total: 5

Calculation Steps:

  1. Initial Entropy:

    Entropy(S) = - (9/14)log2(9/14) - (5/14)log2(5/14) ≈ 0.940 bits

  2. Entropy of Splits:

    Entropy(Sunny) = - (2/5)log2(2/5) - (3/5)log2(3/5) ≈ 0.971 bits

    Entropy(Overcast) = - (4/4)log2(4/4) - (0/4)log2(0/4) = 0 bits (Pure node)

    Entropy(Rain) = - (3/5)log2(3/5) - (2/5)log2(2/5) ≈ 0.971 bits

  3. Weighted Average Entropy:

    Weighted Avg = (5/14)*0.971 + (4/14)*0 + (5/14)*0.971 ≈ 0.347 + 0 + 0.347 ≈ 0.694 bits

  4. Information Gain:

    IG(S, Outlook) = 0.940 - 0.694 ≈ 0.246 bits

Interpretation: An Information Gain of 0.246 bits suggests that ‘Outlook’ is a reasonably good attribute for splitting the dataset, as it reduces the impurity by a significant amount. If you were to calculate information gain using MATLAB, you would define your data and apply similar entropy functions.

Example 2: Customer Churn Prediction

Consider a telecom company trying to predict customer churn (Yes/No). They want to evaluate the ‘Contract Type’ attribute (Month-to-month, One year, Two year).

Initial Dataset:

  • Total Samples: 1000
  • Churn = Yes (Positive): 200
  • Churn = No (Negative): 800

Attribute ‘Contract Type’ Splits:

  • Month-to-month (Split 1):
    • Churn Yes: 150
    • Churn No: 350
    • Total: 500
  • One year (Split 2):
    • Churn Yes: 40
    • Churn No: 260
    • Total: 300
  • Two year (Split 3):
    • Churn Yes: 10
    • Churn No: 190
    • Total: 200

Calculation Steps:

  1. Initial Entropy:

    Entropy(S) = - (200/1000)log2(200/1000) - (800/1000)log2(800/1000) ≈ 0.722 bits

  2. Entropy of Splits:

    Entropy(Month-to-month) = - (150/500)log2(150/500) - (350/500)log2(350/500) ≈ 0.881 bits

    Entropy(One year) = - (40/300)log2(40/300) - (260/300)log2(260/300) ≈ 0.544 bits

    Entropy(Two year) = - (10/200)log2(10/200) - (190/200)log2(190/200) ≈ 0.286 bits

  3. Weighted Average Entropy:

    Weighted Avg = (500/1000)*0.881 + (300/1000)*0.544 + (200/1000)*0.286 ≈ 0.4405 + 0.1632 + 0.0572 ≈ 0.6609 bits

  4. Information Gain:

    IG(S, Contract Type) = 0.722 - 0.6609 ≈ 0.0611 bits

Interpretation: An Information Gain of 0.0611 bits for ‘Contract Type’ indicates it’s a less powerful predictor than ‘Outlook’ in the previous example, but still provides some information. This value would be compared against other attributes to find the best split. When you calculate information gain using MATLAB, you’d follow these exact steps programmatically.

How to Use This Information Gain Calculator

This calculator is designed to help you quickly calculate information gain for a given attribute split. Follow these steps to get your results:

Step-by-Step Instructions:

  1. Input Initial Dataset Class Distribution:
    • Initial Positive Samples: Enter the total count of instances belonging to the ‘Positive’ class in your entire dataset (parent node).
    • Initial Negative Samples: Enter the total count of instances belonging to the ‘Negative’ class in your entire dataset (parent node).

    Example: If you have 9 ‘Yes’ and 5 ‘No’ instances in total, enter 9 and 5 respectively.

  2. Input Attribute Split Data:
    • For each attribute value that creates a split (Child Node 1, Child Node 2, Child Node 3), enter the ‘Positive’ and ‘Negative’ sample counts within that specific subset.
    • If your attribute only has two values, leave the ‘Split 3’ fields as 0. The calculator will automatically adjust.

    Example: For ‘Outlook = Sunny’, if you have 2 ‘Yes’ and 3 ‘No’ instances, enter 2 and 3 for Split 1.

  3. Automatic Calculation:

    The calculator updates results in real-time as you type. You can also click the “Calculate Information Gain” button to manually trigger the calculation.

  4. Resetting Values:

    Click the “Reset” button to clear all input fields and restore the default example values.

  5. Copying Results:

    Click the “Copy Results” button to copy the main Information Gain value and all intermediate results to your clipboard for easy sharing or documentation.

How to Read Results:

  • Information Gain: This is the primary result, indicating the reduction in entropy. A higher value means the attribute is more effective for splitting.
  • Initial Entropy: The impurity of the dataset before any split.
  • Weighted Average Entropy (After Split): The average impurity of the child nodes, weighted by their size.
  • Entropy of Split 1, 2, 3: The individual impurity of each child node. A value of 0 indicates a perfectly pure node.

Decision-Making Guidance:

When building a decision tree, you would calculate information gain for multiple attributes. The attribute with the highest Information Gain is typically chosen as the best split at that node. This process is repeated recursively until the tree is fully grown or a stopping criterion is met. This calculator helps you compare the effectiveness of different attributes quickly, a common task when you calculate information gain using MATLAB for model development.

Key Factors That Affect Information Gain Results

Several factors can significantly influence the Information Gain calculated for an attribute. Understanding these helps in better feature selection and model interpretation, especially when you calculate information gain using MATLAB for complex datasets.

  • Class Distribution in Parent Node:

    The initial entropy of the parent node directly impacts the maximum possible Information Gain. If the parent node is already very pure (low entropy), there’s less room for improvement, leading to lower potential Information Gain values for any split. Conversely, a highly impure parent node offers greater potential for high Information Gain.

  • Purity of Child Nodes:

    The more homogeneous (pure) the child nodes are after a split, the lower their individual entropies will be. This reduction in child node entropy directly contributes to a higher Information Gain. An attribute that creates perfectly pure child nodes (entropy = 0) will yield the highest possible Information Gain for that split.

  • Number of Distinct Attribute Values:

    Information Gain has a bias towards attributes with a large number of distinct values. An attribute with many unique values (e.g., an ID number) can create many small, often pure, child nodes. This can artificially inflate its Information Gain, making it seem like a better split than it truly is for generalization. This is a known limitation, often addressed by using Gain Ratio.

  • Size of Child Nodes (Weighting Factor):

    The weighted average entropy considers the proportion of samples in each child node. Larger child nodes have a greater influence on the weighted average. An attribute that creates a few large, pure child nodes will generally have a higher Information Gain than one that creates many small, pure nodes, assuming the overall purity is similar.

  • Presence of Noise or Outliers:

    Noisy data or outliers can distort the class distributions within nodes, leading to inaccurate entropy calculations and, consequently, misleading Information Gain values. Preprocessing steps like outlier detection and handling are crucial for reliable results.

  • Missing Values:

    How missing values are handled can affect Information Gain. If instances with missing values for an attribute are simply discarded, it can reduce the sample size and alter class distributions. More sophisticated methods, like assigning missing values to the most common class or distributing them proportionally, can impact the resulting entropy and gain.

Frequently Asked Questions (FAQ)

Q: What is the difference between Information Gain and Gini Impurity?

A: Both Information Gain (based on Entropy) and Gini Impurity are metrics used to measure the impurity of a node in a decision tree. Information Gain quantifies the reduction in entropy, while 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 subset. They often lead to similar tree structures, but Gini Impurity is computationally less intensive as it doesn’t involve logarithms. When you calculate information gain using MATLAB, you might also encounter Gini impurity functions.

Q: Why is Information Gain biased towards attributes with many values?

A: An attribute with many distinct values can create many small subsets, each of which might be very pure (low entropy) simply because it contains very few samples. This leads to a low weighted average entropy and thus a high Information Gain, even if the attribute isn’t genuinely predictive. For example, an attribute like ‘Customer ID’ would have very high Information Gain but is useless for generalization.

Q: How can I mitigate the bias of Information Gain?

A: The most common way to mitigate this bias is to use Gain Ratio. Gain Ratio normalizes Information Gain by the ‘Split Information’ of the attribute, which measures the breadth and uniformity of the splits. This penalizes attributes that create many small, uneven splits.

Q: Can Information Gain be negative?

A: No, Information Gain cannot be negative. Entropy is always non-negative. Information Gain is calculated as Initial Entropy - Weighted Average Entropy. Since splitting a node can only reduce or maintain its impurity (never increase it), the weighted average entropy of the child nodes will always be less than or equal to the initial entropy. Therefore, Information Gain will always be greater than or equal to zero.

Q: What does an Information Gain of 0 mean?

A: An Information Gain of 0 means that splitting the dataset on that particular attribute does not reduce the impurity (entropy) of the dataset at all. In other words, the attribute provides no useful information for classifying the samples, and the class distribution in the child nodes is proportionally the same as in the parent node.

Q: How does Information Gain relate to decision tree algorithms like ID3?

A: Information Gain is the core criterion used by the ID3 (Iterative Dichotomiser 3) algorithm to select the best attribute for splitting a node. At each step, ID3 calculates the Information Gain for all available attributes and chooses the one that yields the highest gain to make the split. This process is recursive until all nodes are pure or no more attributes are available.

Q: Is Information Gain used in C4.5 or CART algorithms?

A: The C4.5 algorithm, an improvement over ID3, uses Gain Ratio instead of pure Information Gain to overcome the bias towards multi-valued attributes. The CART (Classification and Regression Trees) algorithm typically uses the Gini Impurity criterion for classification tasks.

Q: How would I calculate information gain using MATLAB?

A: In MATLAB, you would typically implement functions for entropy and then for information gain. You’d represent your data as matrices or tables. For entropy, you’d count class occurrences, calculate probabilities, and then apply the -p*log2(p) formula. For information gain, you’d iterate through potential splits, calculate weighted average entropy, and subtract it from the parent entropy. MATLAB’s built-in functions for data manipulation and mathematical operations make this straightforward, often within custom scripts or by leveraging toolboxes like Statistics and Machine Learning Toolbox for decision tree functions that implicitly use these metrics.

Related Tools and Internal Resources

Explore other related tools and articles to deepen your understanding of machine learning and data analysis concepts:

© 2023 Information Gain Calculator. All rights reserved.



Leave a Reply

Your email address will not be published. Required fields are marked *