Implement Forest-Level Mean Decrease in Impurity (MDI) Feature Importances and Their Standard Deviation
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement two functions. `tree_mdi_from_arrays(children_left, children_right, feature, impurity, weighted_n_node_samples, n_features)` computes one tree's mean decrease in impurity (MDI) vector from its node arrays. `forest_mdi_importance(forest)` returns `(importances, std)` for a fitted forest, where `importances` matches the forest's `feature_importances_`.
Each internal node (`children_left[n] != -1`) adds `w[n]*imp[n] - w[l]*imp[l] - w[r]*imp[r]` to its split feature, where `w` is the weighted sample count of each node, `imp` is its impurity, and `l` and `r` are the node's children. Divide the tree's vector by `w[0]`, then normalize it to sum to 1.
The functions must follow these rules:
- A tree vector with a zero total (for example, a single node) is all zeros.
- `forest_mdi_importance` skips trees with `node_count <= 1`.
- `importances` is the mean of the per-tree vectors, renormalized to sum to 1 (if its sum is `> 0`).
- `std` is the population standard deviation (`ddof=0`) of the per-tree vectors, not renormalized.
- If no tree is eligible, return two zero arrays of length `n_features`.
For example:
```python
children_left = [1, -1, 3, -1, -1]
children_right = [2, -1, 4, -1, -1]
feature = [1, -2, 0, -2, -2]
impurity = [0.5, 0.0, 0.3, 0.0, 0.0]
weighted_n = [10, 4, 6, 3, 3]
tree_mdi_from_arrays(children_left, children_right, feature, impurity,
weighted_n, n_features=3)
# [0.36, 0.64, 0.0]
```
The root splits on feature 1 and removes `10*0.5 - 4*0.0 - 6*0.3 = 3.2`. Node 2 splits on feature 0 and removes `6*0.3 - 3*0.0 - 3*0.0 = 1.8`. After dividing by 10 and normalizing, feature 0 gets 1.8/5 = 0.36 and feature 1 gets 3.2/5 = 0.64.
```hint Trace one tree first
Before writing the forest code, list each internal node's decrease on the example, credit it to the split feature, and check that your vector sums to 1.
```
```hint Which trees count
Work out what a single-node tree would contribute if you kept it as a zero vector. Check whether it would change the renormalized mean, the standard deviation, or both.
```
### Constraints & Assumptions
- `forest` is a fitted scikit-learn forest. Read `forest.estimators_`, each estimator's `tree_` (`children_left`, `children_right`, `feature`, `impurity`, `weighted_n_node_samples`, `node_count`), and `forest.n_features_in_`.
- A node is a leaf when `children_left[node] == -1`. A leaf's `feature` is `-2` and must be ignored.
- Weighted sample counts may be floats.
- `importances` must match `feature_importances_` to floating-point tolerance.
### What a Strong Answer Covers
- A correct per-tree vector: leaves skipped, weighted decreases credited to the split feature, division by `w[0]`, and normalization guarded against a zero total
- Skipping trees with `node_count <= 1`, and returning two zero arrays when no tree is eligible
- Averaging and renormalizing the per-tree vectors for `importances`, while computing `std` with `ddof=0` on the per-tree vectors as they are
- Why these steps reproduce scikit-learn's `feature_importances_`
- Time and space complexity for T trees with up to N nodes and F features
### Follow-up Questions
- Why is MDI biased toward high-cardinality features?
- How would you get an alternative that avoids this bias?
- Why does skipping single-node trees change `std` but not `importances`?
Overview: Compute random forest feature importances by mean decrease in impurity from each tree's node arrays, along with their standard deviation across trees. It tests per-tree normalization, which trees to exclude, averaging with renormalization, and the population standard deviation.