#352Permutation Feature ImportanceMediumML System DesignEvaluation MetricsAsked atMeta · Google · Amazon
Permutation Feature Importance
Background
Permutation importance measures each feature's marginal contribution to a model's predictive performance: shuffle the column, rescore, and the drop in metric equals that feature's importance. Model-agnostic, works for any score function — but compute scales as .
Problem statement
Implement permutation_importance(model, X, y, metric, seed=0):
- Score the baseline:
baseline = metric(y, model.predict(X)). - For each feature : copy
X, shuffle column , scores_i = metric(y, model.predict(X_perm)), recordimp_i = baseline - s_i.
Higher importance = bigger metric drop when shuffled = feature matters more.
Input
model— object with.predict(X) -> y_hat.X—np.ndarrayshape .y— 1-D array of length .metric(y_true, y_pred)— higher = better.seed—int, shuffle RNG seed.
Output
Returns np.ndarray of length , one importance per feature.
Examples
Example 1 — important feature gets higher score
Input: y depends only on X[:, 0]; X[:, 1] is noise
Output: imp[0] > imp[1]
Example 2 — output length matches feature count
Input: X shape (50, 5)
Output: shape (5,)
Constraints
- Use
np.random.default_rng(seed)for reproducibility. - Original
Xis not mutated. - Don't return early on negative importance — a noisy feature's score may slightly increase the metric on shuffle; report the negative value.
Notes
- Repeats. Production runs repeat the shuffle 5–20 times per feature and report the mean ± std — single-shot importance is noisy.
- Correlated features. If feature A and B are correlated, shuffling A alone may not hurt the model much (B still carries the signal). Shapley values handle this correctly but are vastly more expensive.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example: important feature ranks higher
- •Example: linear model with negative-MSE metric
- •Sample: four-feature classifier keyed on X[:,1]