CART Algorithm

Classification and Regression Trees

A fundamental algorithm for decision tree learning

Key Idea: Recursive binary partitioning of feature space to create decision rules

What is binary partitioning? Think of it as repeatedly dividing your data into two groups:

Imagine sorting people into rooms by asking yes/no questions - that's what CART does with data!

Introduction to CART

Classification and Regression Trees

Binary Splitting Mechanism

How CART Builds Trees

Tree Structure

Anatomy of a Decision Tree

Impurity Measures

Classification Trees

Two common impurity measures for classification tasks:

Gini Impurity: $$G = 1 - \sum_{i=1}^{c} p_i^2$$

Where $p_i$ is the proportion of class $i$ in the node

Entropy: $$H = -\sum_{i=1}^{c} p_i \log_2(p_i)$$

Where $p_i$ is the proportion of class $i$ in the node

Goal: Minimize impurity in child nodes after splitting

Impurity Measures

Regression Trees

For regression tasks, we use variance-based measures:

Mean Squared Error (MSE): $$\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \bar{y})^2$$

Where $y_i$ are the target values and $\bar{y}$ is their mean

Alternative measures:

Information Gain

Evaluating Split Quality

Information Gain: $$\text{IG} = \text{Impurity(parent)} - \sum_{j=1}^{k} \frac{n_j}{n} \text{Impurity(child}_j)$$

Where $n_j$ is the number of samples in child $j$

Split Selection:

Algorithm Implementation

CART in Practice

  1. Calculate impurity for current node
  2. For each feature, find optimal split threshold
  3. Select split with maximum information gain
  4. Create child nodes and recurse
  5. Assign predictions to leaf nodes
Pseudocode:
function BuildTree(data, depth):
    if StoppingCriteriaMet(data, depth):
        return LeafNode(data)
    bestFeature, bestThreshold = FindBestSplit(data)
    leftData, rightData = SplitData(data, bestFeature, bestThreshold)
    leftChild = BuildTree(leftData, depth+1)
    rightChild = BuildTree(rightData, depth+1)
    return Node(bestFeature, bestThreshold, leftChild, rightChild)

# Node function creates a decision node in the tree
# function Node(feature, threshold, leftChild, rightChild):
# return {'feature': feature, 'threshold': threshold,
# 'left': leftChild, 'right': rightChild, 'isLeaf': false}

Practical Example

Classification Problem

Let's see how CART partitions a 2D feature space for classification:

Decision Boundaries: Notice how CART creates axis-parallel decision boundaries, resulting in a rectangular partition of the feature space.

Decision Path

Following a Sample Through the Tree

Interpretation: Each decision path forms a rule that can be easily understood and explained:
IF age > 30 AND income <= 50K AND education_years > 12 THEN class = 1

Practical Considerations

Working with CART

Pruning

Controlling Complexity

Cost-Complexity Pruning (Minimal Cost-Complexity Pruning):

$$R_\alpha(T) = R(T) + \alpha \cdot |T|$$

Where $R(T)$ is the error of tree $T$, $|T|$ is the number of leaf nodes, and $\alpha$ is the complexity parameter

Key Takeaways

CART Algorithm

Slide 1 of 13