Sub topic 3
Pruning: Pre-Pruning and Cost-Complexity
Assume you know Python, features and labels, training and test sets, and the difference between classification and regression.
Controlling tree complexity
A fully grown tree can memorize training examples. Pruning reduces this complexity.
Pre-pruning stops growth early. Common controls include max_depth, min_samples_split, and min_samples_leaf. These settings are fast and simple, but an early decision can prevent useful later splits.
Cost-complexity pruning grows a larger tree, then removes subtrees. It minimizes:
training_error + ccp_alpha × number_of_leaves
A larger ccp_alpha favors fewer leaves. Select it with cross-validation rather than choosing it from the test set.
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
data = load_iris()
path = DecisionTreeClassifier(random_state=0).cost_complexity_pruning_path(
data.data, data.target
)
scores = []
for alpha in path.ccp_alphas:
tree = DecisionTreeClassifier(ccp_alpha=alpha, random_state=0)
scores.append(cross_val_score(tree, data.data, data.target, cv=5).mean())
best_alpha = path.ccp_alphas[scores.index(max(scores))]
tree = DecisionTreeClassifier(ccp_alpha=best_alpha, random_state=0)
tree.fit(data.data, data.target)
print(best_alpha, tree.get_n_leaves())Hyperparameters and regression
Tune max_depth, min_samples_leaf, min_samples_split, max_features, and ccp_alpha. Cross-validation compares settings on several validation folds. Keep the test set untouched for the final estimate.
A regression tree predicts a number rather than a class. Each leaf usually predicts the mean target value of its training samples. Its split criterion reduces squared error, or equivalently reduces within-node variance. min_samples_leaf is especially useful when noisy targets would otherwise create tiny leaves.
Importance and rules
A tree's impurity-based feature importance adds the weighted impurity decreases caused by each feature. It is useful for a quick summary, but it can favor continuous or high-cardinality features and can divide credit among correlated features. Use permutation importance or held-out performance for stronger evidence.
A prediction follows one path of rules from the root to a leaf:
flowchart TD
A["petal length <= 2.45"] -->|yes| B["class 0"]
A -->|no| C["petal width <= 1.75"]
C -->|yes| D["class 1"]
C -->|no| E["class 2"]A single tree is easy to explain, but unstable: a small data change can produce a different tree. It also has limited smoothness and may generalize poorly.
Where trees go next
Random forests average many trees trained on bootstrapped samples and random feature subsets, reducing variance. Boosting builds trees sequentially, with each new tree correcting earlier errors. These ensembles usually predict better than one tree, but they are harder to inspect.
Exercise
Which setting directly discourages leaves containing very few samples?
Show answer
min_samples_leaf; tune it with cross-validation.