Decision Tree Cost-Complexity Pruning Sweep and Alpha Selection
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement three functions. `pruning_sweep` computes the effective alphas with `cost_complexity_pruning_path` on the training data, fits one `DecisionTreeClassifier` per alpha and returns a `pd.DataFrame` of tree statistics. `select_best_alpha` picks an alpha from that table, and `fit_best_pruned_tree` refits on the chosen alpha.
```python
def pruning_sweep(X_train, y_train, X_val, y_val, random_state=0): ...
def select_best_alpha(table): ...
def fit_best_pruned_tree(X_train, y_train, X_val, y_val, random_state=0): ...
```
They must follow these rules:
- The table has exactly the columns `["alpha", "n_leaves", "depth", "train_accuracy", "val_accuracy"]`, with one row per path alpha in path order.
- `n_leaves` and `depth` are `int64`, and all other columns are `float64`. Accuracy is the fraction of correct predictions.
- Every tree (the one used for the path and every fit) uses the given `random_state` and `ccp_alpha=max(alpha, 0.0)`, which clips tiny negative alphas.
- `select_best_alpha` returns a `float`: the alpha with the highest `val_accuracy`, with ties broken toward the largest alpha.
- `fit_best_pruned_tree` returns `(fitted_tree, best_alpha)`, with the tree refit on the training data.
Example:
```python
X = [[0.0], [1.0], [2.0], [3.0]]; y = [0, 0, 1, 1]
pruning_sweep(X, y, X, y)
# alpha n_leaves depth train_accuracy val_accuracy
# 0 0.0 2 1 1.0 1.0
# 1 0.5 1 0 0.5 0.5
```
Plotting the curves is optional and not graded.
```hint One way to build every tree
Put tree construction in one helper, so the path, every sweep fit and the final refit share the same `random_state` and the same alpha clamp.
```
```hint Tie order
`idxmax` and `argmax` return the first maximum. In path order, that is the smallest alpha among the tied rows.
```
### Constraints & Assumptions
- `X_train` and `X_val` are 2-D numeric arrays (or lists) with the same features, and `y_train` and `y_val` hold class labels.
- Use scikit-learn's `DecisionTreeClassifier`, pandas and NumPy.
### What a Strong Answer Covers
- Computing the pruning path once on the training data with the given `random_state`, and clamping negative alphas before any fit
- One fit per path alpha, recording leaves, depth and both accuracies in path order
- The exact column names, column order and dtypes of the returned DataFrame
- Choosing the alpha with the best validation accuracy, ties going to the largest alpha, and refitting at that alpha
- Why larger alphas are preferred on ties, and the limits of choosing alpha on a single validation split
### Follow-up Questions
- Why break validation ties toward the largest alpha?
- How would you make alpha selection more reliable than a single validation split?
- Why can `ccp_alphas` contain small negative values?
Overview: Sweep the cost-complexity pruning path of a scikit-learn decision tree, record leaves, depth and accuracy for each effective alpha, and pick the alpha with the best validation accuracy. It tests post-pruning, model selection with a tie-breaking rule and exact DataFrame output.