Implement Mean Decrease in Impurity (MDI) Feature Importance from Decision Tree Node Arrays

Quick Overview

Implement mean decrease in impurity feature importance for a fitted decision tree stored as parallel node arrays in the scikit-learn layout. It tests weighted impurity decreases at each split, accumulation per feature, normalization, and the single-leaf edge case.

Implement Mean Decrease in Impurity (MDI) Feature Importance from Decision Tree Node Arrays

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `mdi_importances(children_left, children_right, feature, impurity, n_node_samples, n_features)` for a fitted decision tree given as parallel node arrays (the layout of scikit-learn's `tree_` object). For each internal node, add `w[node]*imp[node] - w[left]*imp[left] - w[right]*imp[right]` to the importance of `feature[node]`, where `w` is `n_node_samples`, `imp` is `impurity`, and `left` and `right` are the node's two children. Return a float array of length `n_features` normalized to sum to 1. For example, this five-node tree splits on feature 1 at the root and on feature 0 at node 2: ```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.5, 0.0, 0.0] n_node_samples = [10, 4, 6, 3, 3] n_features = 3 mdi_importances(children_left, children_right, feature, impurity, n_node_samples, n_features) # array([0.6, 0.4, 0.0]) ``` Feature 1 gets `10*0.5 - 4*0.0 - 6*0.5 = 2` from the root, and feature 0 gets `6*0.5 - 3*0.0 - 3*0.0 = 3` from node 2. Dividing by the total of 5 gives 0.6 for feature 0, 0.4 for feature 1 and 0.0 for feature 2, which is never used. ```hint Where the work happens Only internal nodes contribute. Find a test that identifies a leaf from the child arrays alone, and never use a leaf's `feature` entry as an index. ``` ```hint Normalization edge case Work out what the sum of all decreases is for a tree that never splits, and what dividing by it would return. ``` ### Constraints & Assumptions - A node is a leaf if and only if `children_left[node] == -1`. A leaf's `feature` is `-2` and must be ignored. - Inputs may be Python lists or NumPy arrays. `n_node_samples` may be float (weighted counts). - A feature used at several nodes accumulates all its decreases. - If the total decrease is `<= 0` (for example, a single-leaf tree), return all zeros of shape `(n_features,)`. - Results must match scikit-learn's `feature_importances_` within `rtol=1e-7`. ### What a Strong Answer Covers - Skipping leaves correctly and computing each internal node's weighted impurity decrease from its two children - Accumulating decreases per feature when the same feature splits at several nodes, with unused features left at zero - Normalizing to sum to 1, and returning a zero vector of length `n_features` when the total is not positive - How the result relates to scikit-learn's own scaling of the decreases, and which sample-count array to pass when the tree was fit with sample weights - Time and space complexity in terms of the number of nodes and features ### Follow-up Questions - Why is MDI biased toward high-cardinality features? - How would you extend this to a random forest? - What alternative avoids MDI's biases, and what does it cost?

Overview: Implement mean decrease in impurity feature importance for a fitted decision tree stored as parallel node arrays in the scikit-learn layout. It tests weighted impurity decreases at each split, accumulation per feature, normalization, and the single-leaf edge case.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement mdi_importances(children_left, children_right, feature, impurity, n_node_samples, n_features) for a fitted decision tree given as parallel node arrays (the layout of scikit-learn's tree_ object). For each internal node, add w[node]*imp[node] - w[left]*imp[left] - w[right]*imp[right] to the importance of feature[node], where w is n_node_samples, imp is impurity, and left and right are the node's two children. Return a float array of length n_features normalized to sum to 1.

For example, this five-node tree splits on feature 1 at the root and on feature 0 at node 2:

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.5, 0.0, 0.0]
n_node_samples = [10, 4, 6, 3, 3]
n_features     = 3

mdi_importances(children_left, children_right, feature, impurity,
                n_node_samples, n_features)
# array([0.6, 0.4, 0.0])

Feature 1 gets 10*0.5 - 4*0.0 - 6*0.5 = 2 from the root, and feature 0 gets 6*0.5 - 3*0.0 - 3*0.0 = 3 from node 2. Dividing by the total of 5 gives 0.6 for feature 0, 0.4 for feature 1 and 0.0 for feature 2, which is never used.

Constraints & Assumptions

  • A node is a leaf if and only if children_left[node] == -1 . A leaf's feature is -2 and must be ignored.
  • Inputs may be Python lists or NumPy arrays. n_node_samples may be float (weighted counts).
  • A feature used at several nodes accumulates all its decreases.
  • If the total decrease is <= 0 (for example, a single-leaf tree), return all zeros of shape (n_features,) .
  • Results must match scikit-learn's feature_importances_ within rtol=1e-7 .

What a Strong Answer Covers Guidance

  • Skipping leaves correctly and computing each internal node's weighted impurity decrease from its two children
  • Accumulating decreases per feature when the same feature splits at several nodes, with unused features left at zero
  • Normalizing to sum to 1, and returning a zero vector of length n_features when the total is not positive
  • How the result relates to scikit-learn's own scaling of the decreases, and which sample-count array to pass when the tree was fit with sample weights
  • Time and space complexity in terms of the number of nodes and features

Follow-up Questions Guidance

  • Why is MDI biased toward high-cardinality features?
  • How would you extend this to a random forest?
  • What alternative avoids MDI's biases, and what does it cost?