Aggregate Feature Importance Across Boosted Trees

Quick Overview

Write a NumPy function that aggregates split count, gain and cover importances over the split nodes of a boosted tree ensemble, with optional normalization. It tests vectorized accumulation with repeated indices, leaf handling, and safe division for unused features.

Aggregate Feature Importance Across Boosted Trees

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `feature_importance(trees, n_features, importance_type="gain", normalize=False)`. Each tree is a dict of equal-length arrays `feature` (int, `-1` for a leaf), `gain` and `cover`. Aggregate over all split nodes of all trees (leaves are ignored entirely) and return a float array of shape `(n_features,)`. - `"weight"`: number of splits on the feature. - `"total_gain"` / `"total_cover"`: sum of split gain / cover. - `"gain"` / `"cover"`: total divided by split count (`0.0` for unused features, never NaN). - `normalize=True`: divide by the sum, but only if the sum is `> 0` (otherwise return unchanged). - Any other `importance_type` raises `ValueError`. Example: ```python t1 = {"feature": [0, 1, -1, -1, -1], "gain": [10, 4, 0, 0, 0], "cover": [8, 5, 3, 2, 3]} t2 = {"feature": [0, -1, 2, -1, -1], "gain": [6, 0, 2, 0, 0], "cover": [8, 4, 4, 2, 2]} feature_importance([t1, t2], 4, "weight") # [2., 1., 1., 0.] feature_importance([t1, t2], 4, "total_gain") # [16., 4., 2., 0.] feature_importance([t1, t2], 4, "gain") # [8., 4., 2., 0.] feature_importance([t1, t2], 4, "cover") # [8., 5., 4., 0.] feature_importance([t1, t2], 4, "gain", normalize=True) # [8/14, 4/14, 2/14, 0.] ``` ```hint Repeated feature indices A tree can split on the same feature at several nodes. Check what your accumulation does when the same index appears more than once in a single vectorized update. ``` ### Constraints & Assumptions - `trees` may be empty (result is all zeros). - Split feature indices lie in `[0, n_features)`. Gain and cover are finite and non-negative. - Arrays may be lists or NumPy arrays. ### What a Strong Answer Covers - Leaves excluded from every total, including cover. - Correct accumulation when one feature is split on several times in the same tree. - Averages that return 0.0 for features that never split, with no division warnings or NaN. - Normalization guarded against a zero sum, and input validation for `importance_type`. - Time and space complexity of the aggregation. ### Follow-up Questions - Why can average `gain` and `weight` rank features very differently? - What are the limitations of these impurity-based importances compared with SHAP or permutation importance? - How would you handle correlated features that share importance?

Overview: Write a NumPy function that aggregates split count, gain and cover importances over the split nodes of a boosted tree ensemble, with optional normalization. It tests vectorized accumulation with repeated indices, leaf handling, and safe division for unused features.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement feature_importance(trees, n_features, importance_type="gain", normalize=False). Each tree is a dict of equal-length arrays feature (int, -1 for a leaf), gain and cover. Aggregate over all split nodes of all trees (leaves are ignored entirely) and return a float array of shape (n_features,).

  • "weight" : number of splits on the feature.
  • "total_gain" / "total_cover" : sum of split gain / cover.
  • "gain" / "cover" : total divided by split count ( 0.0 for unused features, never NaN).
  • normalize=True : divide by the sum, but only if the sum is > 0 (otherwise return unchanged).
  • Any other importance_type raises ValueError .

Example:

t1 = {"feature": [0, 1, -1, -1, -1], "gain": [10, 4, 0, 0, 0], "cover": [8, 5, 3, 2, 3]}
t2 = {"feature": [0, -1, 2, -1, -1], "gain": [6, 0, 2, 0, 0], "cover": [8, 4, 4, 2, 2]}
feature_importance([t1, t2], 4, "weight")      # [2., 1., 1., 0.]
feature_importance([t1, t2], 4, "total_gain")  # [16., 4., 2., 0.]
feature_importance([t1, t2], 4, "gain")        # [8., 4., 2., 0.]
feature_importance([t1, t2], 4, "cover")       # [8., 5., 4., 0.]
feature_importance([t1, t2], 4, "gain", normalize=True)  # [8/14, 4/14, 2/14, 0.]

Constraints & Assumptions

  • trees may be empty (result is all zeros).
  • Split feature indices lie in [0, n_features) . Gain and cover are finite and non-negative.
  • Arrays may be lists or NumPy arrays.

What a Strong Answer Covers Guidance

  • Leaves excluded from every total, including cover.
  • Correct accumulation when one feature is split on several times in the same tree.
  • Averages that return 0.0 for features that never split, with no division warnings or NaN.
  • Normalization guarded against a zero sum, and input validation for importance_type .
  • Time and space complexity of the aggregation.

Follow-up Questions Guidance

  • Why can average gain and weight rank features very differently?
  • What are the limitations of these impurity-based importances compared with SHAP or permutation importance?
  • How would you handle correlated features that share importance?