OOB Permutation Feature Importance with Standard Errors

Quick Overview

Implement out-of-bag permutation feature importance for a set of bagged trees, returning the mean accuracy drop per feature and its standard error across trees. Tests OOB bookkeeping, reproducible seeded shuffles, safe copying of the input matrix, and how to aggregate per-tree estimates.

OOB Permutation Feature Importance with Standard Errors

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `oob_permutation_importance(estimators, inbag_masks, X, y, seed=0)`. For each tree, compute its accuracy on its out-of-bag (OOB) rows (where `inbag_masks[t]` is `False`). Then, for each feature, permute that feature's column among the OOB rows and record the drop `base_acc - perm_acc`. Return `(importances, standard_errors)`: the per-feature mean drop across trees and `std(ddof=1) / sqrt(n_used)`, where `n_used` is the number of trees that contributed. Example: ```python # trees only split on feature 0; X has 3 features imp, se = oob_permutation_importance(trees, masks, X, y, seed=7) imp.shape, se.shape # (3,), (3,) imp[1:], se[1:] # exactly 0.0 (unused features) imp[0] > 0 # True ``` ```hint Where the shuffle happens Each tree has its own OOB rows, and the shuffle happens only among them. Work out how to change one column for one tree while the caller's `X` stays untouched. ``` ```hint Order of random draws Reproducibility depends on the exact sequence of draws. Decide where the check for an empty OOB set sits relative to the first draw for a tree. ``` ### Constraints & Assumptions - Each estimator exposes `predict(X)`. `inbag_masks` must have shape `(n_trees, n_samples)`, otherwise raise `ValueError`. - Randomness: use one `np.random.default_rng(seed)`. Loop over trees in order, then over features `j = 0..d-1`. Draw `rng.permutation(n_oob)` once per (tree, feature) pair. - Skip trees with no OOB rows: they consume no draws. If every tree is skipped, raise `ValueError`. - If only one tree is used, the standard errors are all `0.0`. - Both outputs are float arrays of shape `(n_features,)`. `X` must not be mutated. ### What a Strong Answer Covers - A per-tree baseline accuracy and permuted accuracy, both measured on that tree's OOB rows only. - One seeded generator with draws in the stated order, no draws for skipped trees, and the caller's `X` left unchanged. - Aggregation into the mean drop and `std(ddof=1) / sqrt(n_used)`, with zeros for a single tree and `ValueError` for a mis-shaped mask or no usable tree. - Why features no tree splits on come out at exactly 0.0. - The cost of the tree-by-feature loop, including any copying of the OOB block. ### Follow-up Questions - Why can permutation importance be misleading with correlated features? - How would you reduce the variance of the estimate for a single tree? - How does this measure differ from the impurity-based `feature_importances_` that scikit-learn reports, and why can a value here come out negative?

Overview: Implement out-of-bag permutation feature importance for a set of bagged trees, returning the mean accuracy drop per feature and its standard error across trees. Tests OOB bookkeeping, reproducible seeded shuffles, safe copying of the input matrix, and how to aggregate per-tree estimates.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement oob_permutation_importance(estimators, inbag_masks, X, y, seed=0). For each tree, compute its accuracy on its out-of-bag (OOB) rows (where inbag_masks[t] is False). Then, for each feature, permute that feature's column among the OOB rows and record the drop base_acc - perm_acc. Return (importances, standard_errors): the per-feature mean drop across trees and std(ddof=1) / sqrt(n_used), where n_used is the number of trees that contributed.

Example:

# trees only split on feature 0; X has 3 features
imp, se = oob_permutation_importance(trees, masks, X, y, seed=7)
imp.shape, se.shape   # (3,), (3,)
imp[1:], se[1:]       # exactly 0.0 (unused features)
imp[0] > 0            # True

Constraints & Assumptions

  • Each estimator exposes predict(X) . inbag_masks must have shape (n_trees, n_samples) , otherwise raise ValueError .
  • Randomness: use one np.random.default_rng(seed) . Loop over trees in order, then over features j = 0..d-1 . Draw rng.permutation(n_oob) once per (tree, feature) pair.
  • Skip trees with no OOB rows: they consume no draws. If every tree is skipped, raise ValueError .
  • If only one tree is used, the standard errors are all 0.0 .
  • Both outputs are float arrays of shape (n_features,) . X must not be mutated.

What a Strong Answer Covers Guidance

  • A per-tree baseline accuracy and permuted accuracy, both measured on that tree's OOB rows only.
  • One seeded generator with draws in the stated order, no draws for skipped trees, and the caller's X left unchanged.
  • Aggregation into the mean drop and std(ddof=1) / sqrt(n_used) , with zeros for a single tree and ValueError for a mis-shaped mask or no usable tree.
  • Why features no tree splits on come out at exactly 0.0.
  • The cost of the tree-by-feature loop, including any copying of the OOB block.

Follow-up Questions Guidance

  • Why can permutation importance be misleading with correlated features?
  • How would you reduce the variance of the estimate for a single tree?
  • How does this measure differ from the impurity-based feature_importances_ that scikit-learn reports, and why can a value here come out negative?