Minimal Cost-Complexity Pruning for CART
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
A fully grown binary tree is given as parallel arrays `children_left`, `children_right` (`-1` marks a leaf, node `0` is the root), `impurity` and `weighted_n_node_samples` (written `w` below). Implement `cost_complexity_pruning_path(children_left, children_right, impurity, weighted_n_node_samples)`, which returns `(ccp_alphas, impurities)`, and `prune_tree(children_left, children_right, impurity, weighted_n_node_samples, ccp_alpha)`, which returns the pruned `(children_left, children_right)` for the given `ccp_alpha`.
Use these definitions:
- Node risk is `R(t) = w[t] * impurity[t] / w[0]`. Subtree risk `R(T_t)` is the sum of `R` over the current leaves under `t`.
- The weakest link is the internal node that minimizes `g(t) = (R(t) - R(T_t)) / (n_leaves(T_t) - 1)`. On a tie, the lowest node index wins. Recompute `g` after every cut.
- The path starts with `alpha = 0.0` and the full tree's total leaf risk, then adds one entry `(g, new total leaf risk)` per collapse until the root is a leaf. Tied cuts give repeated alphas. Both returned arrays are `float64`.
- `prune_tree` keeps collapsing the weakest link while `g <= ccp_alpha`. Collapsing sets `-1` for the node and all its descendants. The array length stays the same, and the inputs are not mutated.
- Raise `ValueError` when `ccp_alpha < 0` or when the arrays are not 1-D of equal length.
Example:
```python
left = [1, 3, -1, -1, -1]
right = [2, 4, -1, -1, -1]
imp = [0.5, 0.3, 0.4, 0.1, 0.0]
w = [10., 6., 4., 3., 3.]
cost_complexity_pruning_path(left, right, imp, w)
# -> approximately ([0.0, 0.15, 0.16], [0.19, 0.34, 0.5])
prune_tree(left, right, imp, w, 0.1555)
# -> ([1, -1, -1, -1, -1], [2, -1, -1, -1, -1])
```
```hint Subtree statistics
For each internal node you need the number of current leaves under it and their summed risk. One post-order pass over the nodes reachable from the root gives both.
```
```hint Test the example by hand
At the start, the root's `g` is 0.155, which is below `ccp_alpha = 0.1555`, yet the expected pruned tree keeps the root's split. Work out why before you write the pruning loop.
```
```hint Two comparisons
One comparison picks the weakest link when two nodes share the same `g`, and another decides when pruning stops. Check which of them is strict.
```
### Constraints & Assumptions
- Every internal node has exactly two children, and `w[0] > 0`.
- Results must match scikit-learn's `cost_complexity_pruning_path` and `ccp_alpha` pruning to `rtol=1e-7`.
- Trees have up to a few hundred nodes.
### What a Strong Answer Covers
- Node risk, subtree risk and leaf counts computed over the current tree and normalized by the root weight.
- Choosing the weakest link with the lowest-index tie rule and recomputing `g` after each collapse.
- The path in the required format, including the starting entry and repeated alphas on ties.
- Pruning on copies with the `g <= ccp_alpha` rule, marking every descendant of a collapsed node, and validating the inputs.
- The time and space complexity of the approach.
### Follow-up Questions
- How would you pick `ccp_alpha` in practice?
- What is the complexity of your path computation, and how could it be improved?
- Why must `g` be recomputed after each cut instead of reusing the initial values?
Overview: Implement minimal cost-complexity pruning for a CART tree stored as scikit-learn style parallel arrays: compute the full path of alphas and leaf impurities, then prune the tree at a chosen ccp_alpha. It tests weakest-link pruning, subtree statistics, tie rules and matching scikit-learn's results exactly.