2.3.3 · D3Tree-Based & Instance Methods

Worked examples — Gini impurity

3,112 words14 min readBack to topic

Read the Hinglish version if that's easier.

The scenario matrix

Before working examples, let's list every distinct situation Gini can face. Each row is a "cell". Our examples below will each be tagged with the cell it fills.

Cell Situation What makes it special
C1 Perfectly pure node () hits its floor: exactly
C2 Balanced 2-class () hits its 2-class ceiling:
C3 Skewed 2-class () Between floor and ceiling — the common case
C4 Uniform -class () hits its ceiling for that
C5 Skewed multi-class Concentration lowers below the ceiling
C6 Degenerate: one sample, or empty child Edge input — does the formula even work?
C7 Split decision (compare two splits) Weighted Gini + gain choose the winner
C8 Real-world word problem Translate reality → proportions →
C9 Exam twist: recover from a given Run the formula backwards
Recall Why enumerate cells at all?

Because a "smallest possible" or "largest possible" input often behaves differently from a typical one. A test that never checks the empty child (C6) will crash in real code. Covering the matrix is how you make sure your understanding has no holes.

Let's define the three things we'll use over and over. Two describe a single node; the third scores a whole split.

Whenever we evaluate a split (a cut that sends the parent's items into several child nodes), we need one more formula. It is worth stating on its own, because Cells C6–C8 all lean on it.

Everything below is just these formulas, applied honestly to every cell.


Cells C1–C4: the four "corner" nodes

The picture below plots for a 2-class node () as the first class's proportion slides from to . Watch how the corners of our matrix land on this curve.

Figure — Gini impurity

The next figure shows how the ceiling climbs as classes multiply — and why more classes can be "more confusing".

Figure — Gini impurity

Cell C5: concentration beats spreading


Cell C6: degenerate & edge inputs

Real code hands Gini weird nodes: a single sample, or an empty child after a split. Does the formula survive?


Cell C7: the split decision, fully worked

This is the whole point of Gini: comparing splits with the weighted-split formula. The figure shows a parent node being cut two different ways.

Figure — Gini impurity

Cell C8: a real-world word problem


Cell C9: the exam twist — run Gini backwards


Active Recall

Recall

A node has 3 classes with counts out of 100. Gini? ::: Two children with sizes 40 and 60 have Ginis and . Weighted split Gini? ::: Which cell does an empty child fall in, and why doesn't it break the formula? ::: C6 (degenerate); its weight zeroes the term before is ever evaluated. A 2-class node has . What are its proportions? ::: — the unique 2-class maximum. Why does a skewed 3-class node have lower than a uniform 3-class node? ::: Squaring rewards concentration, so a dominant class raises , lowering .

Connections

  • Gini impurity — parent note (formula, derivation, mnemonics).
  • Decision Trees — where these split comparisons (C7) actually run.
  • Entropy and Information Gain — the same examples with instead.
  • Overfitting and Pruning — why chasing the pure children of C1/Split A can backfire.
  • Random Forests — many Gini-grown trees averaged.
  • Classification vs Regression Trees — regression swaps Gini for variance.
  • Gini Coefficient (Economics) — different concept, same name (see parent's mistake box).

Concept Map

corners C1 to C5

decisions C7 C8

feeds

parent minus split

guards

Scenario Matrix

Single Node Gini

Weighted Split Gini

Gini Gain

Edge cases C6