CART (Classification and Regression Trees) › Formula - Classification
Machine Learning from ScratchAll chapters CART (Classification and Regression Trees) › Formula - ClassificationOne section · Sections in this chapter2 / 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
The mathematical foundation of CART revolves around finding the optimal split that minimizes impurity for classification or variance for regression. Let's walk through these concepts step by step.
Gini Impurity
For classification problems, CART uses the Gini impurity measure to evaluate the quality of a split. The Gini impurity measures how often a randomly chosen element would be incorrectly labeled if it were randomly labeled according to the distribution of labels in the subset.
We start with the basic Gini impurity formula for a node:
where:
- is the Gini impurity for node (a measure of impurity ranging from 0 for pure nodes to a maximum value when classes are evenly distributed)
- represents the set of samples at the current node (the subset of training data that reaches this node)
- is the number of classes (total number of distinct class labels in the classification problem)
- is the proportion of samples belonging to class in the set (calculated as where is the number of samples of class and is the total number of samples in node )
- is the class index (ranging from 1 to )
The reason we square the probabilities is to penalize nodes where the class distribution is mixed. When all samples belong to the same class (complete purity), for one class and for all others, making . When classes are evenly distributed, the Gini impurity reaches its maximum.
To find the best split, we calculate the weighted average of the Gini impurities of the resulting child nodes:
where:
- is the weighted Gini impurity after splitting node (the combined impurity of both child nodes, weighted by their sizes)
- is the left child node after the split (the subset of samples that satisfy the split condition)
- is the right child node after the split (the subset of samples that do not satisfy the split condition)
- is the number of samples in the parent node (total samples before splitting)
- is the number of samples in the left child node (samples satisfying the split condition)
- is the number of samples in the right child node (samples not satisfying the split condition, where )
- and are the weights (proportions of samples going to each child node, so larger child nodes have more influence on the overall impurity measure)
The algorithm selects the split that minimizes this weighted Gini impurity. This makes intuitive sense: we want splits that create child nodes with as pure class distributions as possible.

Regression: Mean Squared Error
For regression problems, CART minimizes the variance (or equivalently, the mean squared error) of the target variable within each leaf node. The variance for a node is calculated as:
where:
- is the variance of the target variable in node (a measure of how spread out the target values are around their mean)
- is the number of samples in node (total count of data points in this node)
- is the target value for sample (the actual observed response value for the -th sample in node )
- is the mean target value in the node (the average of all target values in node )
- indicates summation over all samples in node
The mean target value is calculated as:
where:
- is the mean target value (the arithmetic average of all target values in node , which becomes the prediction for this node)
- is the normalization factor (ensures we compute the average rather than the sum)
- is the sum of all target values in node
The mean squared error is essentially the same as variance, just without the division by :
where:
- is the mean squared error for node (the sum of squared deviations from the mean, used as an alternative to variance)
- is the squared deviation of sample from the mean (measures how far each sample's target value is from the node's average)
For a split, we calculate the weighted sum of the MSEs of the child nodes:
where:
- is the total mean squared error after splitting node (the sum of MSEs from both child nodes)
- is the mean squared error of the left child node (calculated as )
- is the mean squared error of the right child node (calculated as )
This is equivalent to the weighted variance approach used in classification, but since we're dealing with continuous values, we use the sum of squared deviations from the mean.
Split Selection Process
The algorithm evaluates all possible splits for each feature and selects the one that provides the maximum reduction in impurity or variance. For a continuous feature with values , we consider splits of the form for all possible thresholds . For categorical features, we consider all possible subsets of categories.
The information gain (or variance reduction) for a split is:
where:
- is the information gain (the reduction in impurity achieved by the split, with higher values indicating better splits)
- is the impurity of the parent node before splitting (either Gini impurity for classification or variance for regression)
- is the weighted impurity after splitting (the combined impurity of both child nodes)
For classification, this becomes:
where:
- is the Gini impurity of the parent node before splitting
- is the weighted Gini impurity after splitting (calculated as )
For regression, this becomes:
where:
- is the variance of the parent node before splitting
- is the weighted variance of the left child node
- is the weighted variance of the right child node
The algorithm selects the split with the maximum gain.

Mathematical Properties
The Gini impurity and variance measures are monotonically decreasing as we make better splits. This property ensures that the algorithm chooses splits that improve the overall model quality.
The impurity functions are convex, meaning that the weighted average of impurities from child nodes is less than or equal to the parent node's impurity. This ensures that splitting does not increase the overall impurity.
For each feature, the algorithm must evaluate all possible split points. For continuous features with unique values, this requires evaluations. For categorical features with categories, it requires evaluating possible splits, making it computationally expensive for high-cardinality categorical features.
where:
- is the number of unique values for a continuous feature (the distinct values that could serve as split thresholds)
- represents linear computational complexity (evaluating each unique value as a potential split point)
- is the number of categories for a categorical feature (the distinct category labels)
- is the number of possible binary partitions of categories (exponential growth with the number of categories)
The computational complexity of CART grows significantly with the number of features and their cardinality. For a dataset with features and samples, the worst-case time complexity is for continuous features, but can be much higher for categorical features with many categories.
where:
- is the number of features (predictor variables in the dataset)
- is the number of samples (data points in the training set)
- represents the computational complexity (the algorithm must evaluate features, and for each feature with samples, sorting takes time)
This is why preprocessing steps like feature selection or categorical encoding are often necessary for large-scale applications.