Open In App

How to Calculate Gini Index in Decision Tree?

Answer: To calculate the Gini index in a decision tree, compute the sum of squared probabilities of each class subtracted from one.

To calculate the Gini index in a decision tree, follow these steps:

  1. Calculate Gini Impurity for Each Node:
    • For a node t containing Nt​ data points, calculate the Gini impurity (G(t)) using the formula:
    • Where pi​ is the proportion of data points in node t belonging to class i, and c is the number of classes.
  2. Calculate Weighted Gini Impurity for Each Split:
    • For each split point (based on an attribute), calculate the weighted sum of Gini impurities of the resulting child nodes.
    • Weighted Gini Impurity , where Nleft​ and Nright​ are the number of data points in the left and right child nodes, respectively, and N is the total number of data points.
  3. Select the Split with the Lowest Gini Index:
    • Choose the attribute and split point that result in the lowest weighted Gini impurity as the optimal split for the current node in the decision tree.
  4. Repeat for All Attributes:
    • Repeat the above steps for all available attributes to find the attribute that minimizes the Gini index across all possible splits.

Conclusion:

The Gini index measures the impurity of a dataset, with lower values indicating a purer (more homogeneous) node. By selecting splits that minimize the weighted Gini impurity, decision trees can effectively partition the data into subsets that are more homogeneous with respect to the target variable.

Article Tags :