Pruning (pre-pruning, post-pruning)
Intermediate classical-mldecision-treesregularization
Implement two functions:
def build_tree_pre_pruned(
input: np.ndarray, labels: np.ndarray, max_depth: int, min_samples_leaf: int = 1
) -> dict:
"""Like build_tree, plus: never take a split that leaves a child
with fewer than min_samples_leaf samples. Every node also carries
a "default" field: the majority class of training labels at that
node."""
def prune_tree(tree: dict, input_val: np.ndarray, labels_val: np.ndarray) -> dict:
"""Reduced-error post-pruning against a held-out validation set."""
build_tree_pre_pruned reuses 03-best-split-minimal-tree's find_best_split and stopping logic, with min_samples_leaf as one more stopping rule.
prune_tree never mutates the tree it's given, it returns a new (possibly smaller) tree.