Feature importance from a fitted tree
Intermediate classical-mldecision-treesinterpretability
Implement:
def feature_importances(
tree: dict, input: np.ndarray, labels: np.ndarray, n_features: int
) -> np.ndarray:
"""
tree: a tree from build_tree (03-best-split-minimal-tree), fitted
on (input, labels).
Returns:
shape (n_features,), summing to 1 (or all zeros for a
single-leaf tree).
"""
- Reuse
02-information-gain's information_gain, don't reimplement it.
- Re-derive each node's data subset by walking the tree with
input/labels, the same way predict_tree walks it, rather than assuming the tree stores its own data.