CART (Classification and Regression Trees) › Example

Michael BrenndoerferJune 27, 20254 min read
Machine Learning from ScratchAll chapters
Part 5 of 9Tree-Based Models
Chapter 41CART (Classification and Regression Trees) › Example
Reading section 5 of 9 · Example · 4 minSection 5 of 9 · 4 min

Book contents

Section 5 of 9 · 4 min read

CART (Classification and Regression Trees) › ExampleOne section · Sections in this chapter5 / 9 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete example to understand how CART builds a decision tree step by step. We'll use a simple classification problem with two features and three classes.

Dataset Setup

Consider the following training data:

SampleFeature 1 (X₁)Feature 2 (X₂)Class
12.51.2A
23.12.8B
31.83.2A
44.21.5C
52.92.1B
61.52.9A
73.82.3C
82.21.8A

Step 1: Calculate Initial Gini Impurity

First, we calculate the Gini impurity for the root node (all samples):

  • Class A: 4 samples (50%)
  • Class B: 2 samples (25%)
  • Class C: 2 samples (25%)
Giniroot=1−(0.52+0.252+0.252)=1−(0.25+0.0625+0.0625)=1−0.375=0.625Gini_{root} = 1 - (0.5^2 + 0.25^2 + 0.25^2) = 1 - (0.25 + 0.0625 + 0.0625) = 1 - 0.375 = 0.625

Step 2: Evaluate All Possible Splits

Split on X₁ ≤ 2.5:

Left child (X₁ ≤ 2.5): Samples 1, 3, 6, 8

  • Class A: 4 samples (100%)
  • Class B: 0 samples (0%)
  • Class C: 0 samples (0%)
Ginileft=1−(1.02+02+02)=1−1.0=0Gini_{left} = 1 - (1.0^2 + 0^2 + 0^2) = 1 - 1.0 = 0

Right child (X₁ > 2.5): Samples 2, 4, 5, 7

  • Class A: 0 samples (0%)
  • Class B: 2 samples (50%)
  • Class C: 2 samples (50%)
Giniright=1−(02+0.52+0.52)=1−(0+0.25+0.25)=1−0.5=0.5Gini_{right} = 1 - (0^2 + 0.5^2 + 0.5^2) = 1 - (0 + 0.25 + 0.25) = 1 - 0.5 = 0.5

Weighted Gini impurity:

Ginisplit=48×0+48×0.5=0.5×0+0.5×0.5=0+0.25=0.25Gini_{split} = \frac{4}{8} \times 0 + \frac{4}{8} \times 0.5 = 0.5 \times 0 + 0.5 \times 0.5 = 0 + 0.25 = 0.25

Information gain:

Gain=0.625−0.25=0.375Gain = 0.625 - 0.25 = 0.375

Split on X₂ ≤ 2.0:

Left child (X₂ ≤ 2.0): Samples 1, 4, 8

  • Class A: 2 samples (66.7%)
  • Class B: 0 samples (0%)
  • Class C: 1 sample (33.3%)
Ginileft=1−(0.6672+02+0.3332)=1−(0.445+0+0.111)=0.444Gini_{left} = 1 - (0.667^2 + 0^2 + 0.333^2) = 1 - (0.445 + 0 + 0.111) = 0.444

Right child (X₂ > 2.0): Samples 2, 3, 5, 6, 7

  • Class A: 2 samples (40%)
  • Class B: 2 samples (40%)
  • Class C: 1 sample (20%)
Giniright=1−(0.42+0.42+0.22)=1−(0.16+0.16+0.04)=0.64Gini_{right} = 1 - (0.4^2 + 0.4^2 + 0.2^2) = 1 - (0.16 + 0.16 + 0.04) = 0.64

Weighted Gini impurity:

Ginisplit=38×0.444+58×0.64=0.375×0.444+0.625×0.64=0.1665+0.4=0.5665Gini_{split} = \frac{3}{8} \times 0.444 + \frac{5}{8} \times 0.64 = 0.375 \times 0.444 + 0.625 \times 0.64 = 0.1665 + 0.4 = 0.5665

Information gain:

Gain=0.625−0.5665=0.0585Gain = 0.625 - 0.5665 = 0.0585

Step 3: Select Best Split

The split on X₁ ≤ 2.5 provides the highest information gain (0.375), so we choose this as our first split.

Step 4: Continue Building the Tree

We now have two child nodes. The left child (X₁ ≤ 2.5) is already pure (all samples are class A), so it becomes a leaf node. The right child (X₁ > 2.5) still has mixed classes and needs further splitting.

For the right child, we repeat the process:

  • Current samples: 2, 4, 5, 7
  • Classes: B, C, B, C

The best split would be on X₂ ≤ 2.1, which would separate the classes perfectly.

Final Tree Structure

Root: X₁ ≤ 2.5? ├── Yes → Class A (Leaf) └── No → X₂ ≤ 2.1? ├── Yes → Class B (Leaf) └── No → Class C (Leaf)

This example demonstrates how CART greedily selects splits that maximize information gain at each step, ultimately creating a tree that perfectly classifies the training data.