Trees
Regression Trees¶
| Symbol | Meaning |
|---|---|
| number of terminal leaves (or rectangles or nodes). Optimal is determined with a minimizing criterion. | |
| predicted value in terminal leaf (or rectangle ) | |
| indicator of whether a test observation falls into that terminal leaf | |
| p-dimensional vector | |
| a region that corresponds to leaf |
Fitting¶
Use recursive binary splitting to build a decision tree.
Recursive:
At each node, solving the same problem as before. For example, “How do I best split this data?”
Instead of fitting a different algorithm for each level of the tree, write one splitting rule and call it repetitively on each resulting subset.
Binary:
At every node we ask one yes, no question, and produce exactly 2 branches.
Best split is chosen across all variables and all vaues for each variable such that the residual sums of square (RSS) is minimized.
Every leaf region, , gets the same prediction; a single constant, . Hence, for all points in that leaf.
Similar to linear regression in a way that we fit the intercept-only model, such as .
Minimizing RSS here: Use least squares. .
Here, the estimator () is the average; . ’
In regression trees, we are fitting an itnercept-only model to every single leaf.
Since can only be in one region at a time, exactly one indicator term equals 1 and the rest will be 0, so the sum just collapses to a single .
Best Split¶
Goal: Split into 2 regions on variable and splitting value .
Find variable and splitting value that minimizes:
Comparing all three candidate splits¶
For the same five observations, the residual sums of squares are:
| Threshold | Left prediction | Right prediction | Total RSS |
|---|---|---|---|
| 3.5 | 0 | 2 | 22 |
| 4.5 | 3 | ||
| 5.5 | 3 |
The best first split is therefore . Both observations with must go to the same child even though their responses differ. A split on cannot distinguish them.
This is a greedy choice: it gives the best immediate reduction among the candidate splits. It need not be the first split of the globally best tree. Later splits are chosen within the regions already created.
When there are multiple leaves in a tree, we need a way to decide which leaf to split next on which variable. To do so, measure the reduction in SS that each possible split can give you.
It puts all splits on a comparable scale regardless of leaf size, so you’re always making the single most impactful split available.
Stopping Criterion¶
When should a tree be stopped growing to avoid overfitting?
Options¶
(Less preferred) Split only if the split reduces the residual sum of squares (RSS) by at least some threshold value.
Over-build the tree and prune the tree later.
Minimum observations.
Set a threshold that is defined by a number of observations that must be contained in each leaf.
Having less number of observations: High variance (increases the risk of overfitting)and bad bias/variance tradeoff.
Tree Pruning¶
- Pruning
- After builidng on overly large tree, remove leaves that do not substantially improve classification or regression.
After a tree is fully extended, remove one split at a time to minimize this criterion:
| Concept | Description |
|---|---|
| || | Number of leaves in the tree |
| || | Penalty for having too many leaves |
| Tuning parameter, chosen via cross-validation | |
| Indexes leaves | |
| Indexes observations within a given leaf |
Larger tree has less bias but more variance (overfitting).
Smaller tree has more bias but less variance (risk of underfitting).
is a regularization term that regulates bias-variance tradeoff.
What pruning compares¶
A split that slightly improves training fit can still hurt predictions on new observations. Cost-complexity pruning trades the fit improvement against an extra-leaf penalty. When , the criterion gives no penalty for size; increasing favors smaller subtrees.
For a given subtree, pruning a branch removes all of its descendant leaves. The candidate trees are therefore nested subtrees of the grown tree, not arbitrary collections of leaves. Use cross-validation to choose the penalty, rebuilding the tree within each training fold. The final tree is then fitted using the available training data and the selected setting.
Non-Continuous -Variables¶
Ordinal Values¶
Assign numerical value (levels) to each terms.
E.g., <“high scool”, “some college”, “college”> as and treat it as continuous variables.
As long as the order is maintained, it doesn’t matter what value is actually getting used. Hence, spacing between categories is made up, and can be ignored in tree models.
This cannot be done for linear models who assume that the relationship is perfectly linear.
The coefficient gets multiplied by the coded value directly, and a value of 2 has exactly twice the effect of 1.
Indicator Variables¶
- Indicator Variable
- Variables that only take 0 or 1.
Only allows a single split.
Split point is always 0.5, given the indicator variable is coded as either 0 or 1.
Categorical Nominal Variables¶
Some categories can be split off.
E.g., “Hispanic”, “White”, “African American”, “Asian” in
race-variable.Option 1: Split into 2 subsets, such as “White”, “African American” and “Hispanic”, “Asian” based on similiar outcomes.
Option 2: One vs. Rest. Split off a single category from a set of categories.
Classification Trees¶
Prediction¶
Prediction is made on the most frequently occurring class for a given leaf.
is the proportion of training points in the leaf that belongs to class . Used to measure how pure a leaf is. A pure leaf has all points belonging to one class.
E.g.,
In this case, predicted value is history.
Minimization Criterion¶
RSS cannot be used anymore.
Alternative criterias used:
| Measure | Formula | What it means |
|---|---|---|
| Classification error | Fraction of points in the leaf that aren’t the majority class. | |
| Gini index | Probability of misclassifying a randomly chosen point in the leaf | |
| Entropy | Amount of disorder/uncertainty in the leaf |
= indexes outcomes, = indexes leaves.
- Pure Node
- A node is pure if all observations in it belong to the same class.
Why child sizes matter¶
For node impurity , the improvement from a split is
A small pure child should not receive the same weight as a much larger mixed child. In the example above, the unsplit parent has Gini index 0.5. The reductions are and , so the second split is preferred.
Gini impurity is the probability that two independent labels drawn from the node’s class proportions disagree. It is not the error rate of always predicting the majority class. Entropy also measures mixture, using the convention for an absent class.
Leaf probabilities and interactions¶
A leaf with two positives out of two observations has an empirical probability of 1, but very little evidence supports that estimate. A minimum leaf size can make probability estimates less variable.
A path also describes an interaction. If a split on income occurs only inside an age branch, the role of income depends on age. The tree represents this through its sequence of conditions, without explicitly constructing an age-by-income product.
Usage
Can capture non-linear relationships.
Can be highly interpretable.
Less sensitive to extreme predictor magnitudes because splits use ordering; response outliers can still strongly affect regression trees.
Missing data can be handled with suitable split rules or imputation; support depends on the tree method.
Can capture feature interactions naturally without needing interaction terms.
Can use mixed data types.
Limitations
Not useful for linear relationships.
Not useful for small datasets as leaves become too small and unstable.
Not very good at prediction.
A single tree has high variance and can be destabilized by small changes in data.
Highly unstable to perturbations of training data.
Cannot be extrapolated beyond the range of training data.