liveBook · Manning

Gini impurity

As explained in Statistics Every Programmer Needs

Gini impurity is a node-impurity measure equal to the probability of misclassifying an observation when labels are assigned according to the node's class distribution.

Gini impurity measures how mixed the class labels are at a decision-tree node. It is the probability of an incorrect label if an observation is labeled at random according to that node’s class distribution. A pure node has impurity 0; for two equally represented classes, impurity is 0.5. section 6.3.6

For class proportions p₁, p₂, …, calculate Gini impurity as 1 − Σpᵢ². To assess a proposed split, calculate impurity on each side, then average the two results weighted by their numbers of observations. Prefer the candidate with the lower weighted impurity: comparing unweighted values would disregard how many observations each branch contains. section 6.3.6

Compare fourth-down conversion splits

A split at distance to go ≤ 3.5 produces branches with conversion-label counts (127, 268) and (156, 67). Their Gini impurities are approximately 0.436 and 0.420; weighting by branch sizes 395 and 223 gives approximately 0.431. A play-type split, using counts (64, 175) and (219, 160), gives approximately 0.451. Among these two candidates, distance to go better separates conversion outcomes; selecting the root still requires checking the other candidate splits. section 6.3.6

>>> to_go_less_than_3_5 = \
>>>     train_data[train_data['TO_GO'] <= 3.5]    #1
>>> to_go_greater_than_3_5 = \
>>>     train_data[train_data['TO_GO'] > 3.5]     #2
>>> counts_less_than_3_5 = (to_go_less_than_3_5 \
>>>            .groupby('CONVERT').size())  #3
>>> print('Counts for TO_GO <= 3.5:')    #4
>>> print(counts_less_than_3_5)     #5
>>> counts_greater_than_3_5 = (to_go_greater_than_3_5 \
>>>            .groupby('CONVERT').size())     #6
>>> print('Counts for TO_GO > 3.5:')    #7
>>> print(counts_greater_than_3_5)  #8
Counts for TO_GO <= 3.5:
CONVERT
0    127
1    268
dtype: int64
Counts for TO_GO > 3.5:
CONVERT
0    156
1     67
dtype: int64

The pandas grouping code obtains class counts on both sides of the distance-to-go threshold—the inputs needed for the weighted calculation. section 6.3.6

In scikit-learn, `criterion='gini'` selects this split criterion for `DecisionTreeClassifier`; it is also available in `RandomForestClassifier`. Entropy is another classification criterion and can produce different splits. Maximum depth is a separate setting that limits tree growth to help control overfitting. section 6.3.2 section 6.4.1

How Statistics Every Programmer Needs covers it

How deep it goes
Works through class counts and size-weighted impurity for categorical and numeric splits, while treating entropy mainly as an alternative.
Examples
Uses fourth-down conversion counts grouped by play type and by a distance-to-go threshold.
What it assumes
Uses class-label counts and training arrays while introducing the split criterion and classifier fitting directly.
What you can do after
Apply it via scikit-learn classifiers and calculate weighted impurity to compare candidate root splits.
How it connects
Connects decision trees and random forests to split criteria, entropy, information gain, and tree depth.

Also explained in

Compare how 5 books explain Gini impurity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.