Zhou's Website
🌐

Feature Image

Classification and Regression Tree (CART)

Math, Data Science, Machine Learning, Supervised Learning
A tree-based method of building decision trees for predicting categorical variables and can also be used for non-linear prediction
   Last Update:

Introduction

Regression and classification tree (CART) is a tree based method, that partition the feature into a set of regions and then individually fit a model, which can be used for nonlinear variable prediction.

  1. Classification tree: use in predict categorical variables, ex: color, gender
  2. Regression tree: use in predict numerical variables, ex: height, weight

For example, given $X$ to predict $Y$.

nonlinear.png

First, partition feature into a set of regions.

partition.png

Then individually fit a model, like a constant.

fit.png

Regression tree

One Dimension Partition

Considering binary partitioning and constant fitting, each partition should consider the split point $t_m$ (or region $R_m$) and the coefficient ​​$c_m$ of the region.

$$ \hat{y} = f (x) = \sum_{m = 1}^{M} c_m I (x \in R_m). $$

tree.png

Determine $c_m$ ($1 \leq m \leq M$) which minimize the sum of square error (SSE)

$$ \begin{align*} c_m & = \argmin_{c_m} \sum_{i = 1}^{n} (y_i - \hat{y}_i)^2 \\ & = \argmin_{c_m} \sum_{x_i \in R_m} (y_i - c_m)^2 \\ & = \text{mean} (y_i | x_i \in R_m). \end{align*} $$

Next, determine $R_m$ by binary partition

$$ \begin{align*} R_1 (t) = \{ X : X \leq t \} \quad \text{and} \quad R_2 (t) = \{ X : X > t \} \end{align*} $$

Seek the best $t$ also minimize SSE,

$$ \begin{align*} \min_{t} \left[ \min_{c_1} \sum_{x_i \in R_1 (t)} (y_i - c_1)^2 + \min_{c_2} \sum_{x_i \in R_2 (t)} (y_i - c_2)^2 \right] \end{align*} $$

But we won’t consider $t \in \mathbb R$, in stead, only consider $t$ in the range of data points. Define $X_{(i)}$ be the order statistic of $X_1, X_2, \cdots, X_n$, try $t$ from $X_{(2)}$ to $X_{(n - 1)}$, find the $t$ that minimize SSE.

$$ \begin{align*} \min_{t \in X} \left[ \min_{c_1} \sum_{x_i \in R_1 (t)} (y_i - c_1)^2 + \min_{c_2} \sum_{x_i \in R_2 (t)} (y_i - c_2)^2 \right] \end{align*} $$

cut.gif

Multi Dimension Partition

Suppose $X$ has $p$ dimension,

$$ \begin{align*} R_1 (j, t) = \{ X : X_j \leq t \} \quad \text{and} \quad R_2 (j, t) = \{ X : X_j > t \}. \end{align*} $$

Seek the best $j$ and $t$ that minimize SSE,

$$ \begin{align*} \min_{j, t} \left[ \min_{c_1} \sum_{x_i \in R_1 (j, t)} (y_i - c_1)^2 + \min_{c_2} \sum_{x_i \in R_2 (j, t)} (y_i - c_2)^2 \right]. \end{align*} $$

Start $j$ from $1$ to $p$, in particular

$$ \begin{align*} \min_{j \in \{1, 2, \cdots, p\}} \min_{t \in X_j} \left[ \min_{c_1} \sum_{x_i \in R_1 (j, t)} (y_i - c_1)^2 + \min_{c_2} \sum_{x_i \in R_2 (j, t)} (y_i - c_2)^2 \right]. \end{align*} $$

Use iris data set to build a regression tree (numerical part)

Sepal.Length ($X_1$)Sepal.Width ($X_2$)Petal.Length ($X_3$)Petal.Width ($Y$)
5.13.51.40.2
4.93.01.40.2
4.73.21.30.2
4.63.11.50.2
5.03.61.40.2
5.43.91.70.4
4.63.41.40.3
5.03.41.50.2

iris_1.png

Start from $j = 1$, i.e. try to partition $X_1$

iris_2.png

Simalliry, try to partition $X_2$ and $X_3$

iris_3.png

iris_4.png

Finally, find the smallest $j$ and $t$ minimize

$$ \begin{align*} \min_{j \in \{1, 2, 3\}} \min_{t \in X_j} \left[ \min_{c_1} \sum_{x_i \in R_1 (j, t)} (y_i - c_1)^2 + \min_{c_2} \sum_{x_i \in R_2 (j, t)} (y_i - c_2)^2 \right]. \end{align*} $$

iris_5.png

The best cut at $(j, t) = (3, 1.9)$, i.e.

$$ \begin{align*} R_1 = \{ X : X_3 \leq 1.9 \} \quad \text{and} \quad R_2 = \{ X : X_3 > 1.9 \}. \end{align*} $$

best_cut.png

Now mark the point of $R_1$ in red.

best_cut_1.png

Next, remove the red poins, the remain part will be $R_2$.

best_cut_2.png

We can still perform a CART again for remain part, we will get

best_cut_3.png

Repeat the process, until the cut SSE has no obvious minimum value.

Cost Complexity Criterion

Let $|T|$ be the number of terminal nodes in the tree $T$, and let

$$ \begin{align*} N_m & = | R_m |, \\ c_m & = \frac{1}{N_m} \sum_{x_i \in R_m} y_i, \\ Q_m (T) & = \frac{1}{N_m} \sum_{x_i \in R_m} (y_i - c_m)^2. \end{align*} $$

Define the cost complexity criterion, for a given $\alpha \geq 0$,

$$ \begin{align*} C_\alpha (T) = \sum_{m = 1}^{|T|} N_m Q_m (T) + \alpha |T|. \end{align*} $$

Our goal is to minimize this criterion, where $\alpha$ is a tuning parameter, it punishes trees that are too large or too complex. The larger the $\alpha$, the smaller the tree.

Classification Tree

For categorical variable (e.g. color), square error is not available, assume $y$ only take values in $K$ classes, denote by $\\{1, 2, \cdots, K\\}$, for $k = 1, 2, \cdots, K$, use impurity to measure the $Q_m (T)$, let

$$ \begin{align*} \hat p_{mk} = \frac{1}{N_m} \sum_{x_i \in R_m} I (y_i = k) \end{align*} $$

Classify in node $m$ to class

$$ \begin{align*} k (m) = \argmax_{k \in \{1, 2, \cdots, K\}} \hat p_{mk}. \end{align*} $$

There some different impurity measure of $Q_m (T)$,

  1. Misclassification error:

    $$ \begin{align*} \frac{1}{N_m} \sum_{x_i \in R_m} I (y_i \ne k(m)) = 1 - \hat p_{m k (m)}. \end{align*} $$
  2. Gini index:

    $$ \begin{align*} \sum_{k \ne l} \hat p_{m k} \hat p_{m l} = \sum_{k = 1}^{K} \hat p_{m k} (1 - \hat p_{m k}). \end{align*} $$
  3. Cross-entropy or deviance:

    $$ \begin{align*} - \sum_{k = 1}^{K} \hat p_{m k} \ln (\hat p_{m k}). \end{align*} $$

Other Building Method

To build a tree, we have two step to consider:

  1. Partition feature into a set of regions.
  2. Individually fit a model.

Linear Combination Splits

Origin partition consider $X_j < t$, we can splits along the linear combition of the form $\sum_{j = 1}^{p} a_j X_j < t$. To determine $a_j$ and the cut point $t$, that is minimize the Gini index criterion.

linear_combination_splits.png

For example, the first two columns of iris are basically perfect separation.

Regression Fitting Tree

Instead of just constant fitting, we can also fit regression models to different regions individually, and our goal is still to minimize cost complexity criterion (or SSE).

regression_fit.gif

Example in R

# load data
data = iris 

# split train 70% and test 30%
set.seed(0)
samples = sample(1:dim(data)[1], dim(data)[1] * 0.7)
train = data[samples, ]
test = data[-samples, ]

# train model by rpart
library(rpart)
model = rpart(Species ~ ., data = train) 

# confusion matrix
predictions = predict(model, newdata = test, type = "class")
confusion_matrix = table(Actual = test$Species, Predicted = predictions)
print(confusion_matrix) 

# accuracy
accuracy = sum(diag(confusion_matrix)) / sum(confusion_matrix)
print(paste("Accuracy:", accuracy))

# plot model
library(rpart.plot)
rpart.plot(model) 

Result:

            Predicted
Actual       setosa versicolor virginica
  setosa         15          0         0
  versicolor      0         15         1
  virginica       0          2        12
[1] "Accuracy: 0.933333333333333"

rpart.png

References

The Element of Statistical Learning (Trevor Hastie)