Weighted Gini

Learn how weighted Gini combines impurity across left and right child nodes.

A single split creates two child groups—a left group and a right group. Weighted Gini combines their impurity scores into one number the tree can compare.

Big idea

After a split, each child may have a different Gini score and a different number of samples. A tiny but very pure group should not outweigh a large mixed group.

Weighted Gini averages the left and right Gini values, giving more weight to the side with more samples. The tree uses this score to judge whether a split is good.

Formula

Weighted Gini=nleftnGini(left)+nrightnGini(right)\text{Weighted Gini} = \frac{n_{\text{left}}}{n} \cdot \text{Gini}(\text{left}) + \frac{n_{\text{right}}}{n} \cdot \text{Gini}(\text{right})

Here nn is the total number of samples in the parent node, and nleftn_{\text{left}} and nrightn_{\text{right}} are the sizes of each child.

Split rules:

  • Left: samples where xtx \leq t
  • Right: samples where x>tx > t

Worked example

On our 8-sample practice dataset, splitting Feature 0 at threshold t=2.5t = 2.5 gives:

GroupSamplesClass MixGiniWeight
Left (x2.5x \leq 2.5)43 class 0, 1 class 10.3754/8=0.54/8 = 0.5
Right (x>2.5x > 2.5)44 class 10.0004/8=0.54/8 = 0.5
Weighted Gini=48(0.375)+48(0)=0.1875\text{Weighted Gini} = \frac{4}{8}(0.375) + \frac{4}{8}(0) = 0.1875

Common mistake

Averaging Gini values without weighting by sample count. Two children of unequal size need the nleftn\frac{n_{\text{left}}}{n} and nrightn\frac{n_{\text{right}}}{n} factors—otherwise a small group can skew the result.

Key takeaway

Weighted Gini is how a decision tree scores one candidate split. Lower weighted Gini means the split produced cleaner child groups overall.

Use the Weighted Gini Calculator below to adjust left and right class counts and see the weighted score change.

Interactive

Weighted Gini Split Calculator

Compare left and right child groups to see how weighted Gini combines impurity across a split.

Left group

Right group

Left total

10

Right total

10

Total samples

20

Gini(left)

0.480

Gini(right)

0.320

Weighted Gini formula

(10 / 20) × 0.480 + (10 / 20) × 0.320 = 0.400

(nleft / n) × Gini(left) + (nright / n) × Gini(right)

Weighted Gini

0.400

Practice

Try It Yourself

Open the practice lab to complete the starter code in the notebook.

Knowledge Check

Quick Quiz

Weighted Gini accounts for child group size by:

Summary

Key Takeaways

  • A split creates left and right child groups.
  • Weighted Gini averages child impurities by group size.
  • The best split minimizes weighted Gini impurity.