
Classification and Regression Tree (CART)
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.
- Classification tree: use in predict categorical variables, ex: color, gender
- Regression tree: use in predict numerical variables, ex: height, weight
For example, given $X$ to predict $Y$.

First, partition feature into a set of regions.

Then individually fit a model, like a constant.

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). $$
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*} $$
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.1 | 3.5 | 1.4 | 0.2 |
| 4.9 | 3.0 | 1.4 | 0.2 |
| 4.7 | 3.2 | 1.3 | 0.2 |
| 4.6 | 3.1 | 1.5 | 0.2 |
| 5.0 | 3.6 | 1.4 | 0.2 |
| 5.4 | 3.9 | 1.7 | 0.4 |
| 4.6 | 3.4 | 1.4 | 0.3 |
| 5.0 | 3.4 | 1.5 | 0.2 |
| … | … | … | … |

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

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


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*} $$
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*} $$
Now mark the point of $R_1$ in red.

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

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

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)$,
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*} $$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*} $$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:
- Partition feature into a set of regions.
- 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.

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).

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"

References
The Element of Statistical Learning (Trevor Hastie)