Interpretability

Interpretability connects a fitted model’s behavior to features, examples, and decisions. A decision tree is directly readable as rules; a linear model exposes coefficients; a random forest usually needs post-hoc tools such as permutation importance or partial dependence.

Why interpret a model

Interpretability is not decoration after evaluation metrics. It is a debugging tool: which features drive predictions, where does the model rely on shortcuts, and which examples are near a decision boundary? Different questions call for different tools:

MethodScopeWhat it answers
Model coefficientsglobalthe direction and size of each feature’s linear effect
Permutation importanceglobalhow much the model relies on each feature
Partial dependenceglobalthe average effect of a feature across its range
Local attributions (SHAP-style)localwhy this one prediction came out as it did

Measuring feature importance

Permutation importance for feature measures how much the model’s score drops when that feature’s link to the target is destroyed:

where is the fitted model, the feature matrix, the labels, a chosen score (such as accuracy or ), and is with the values in column randomly permuted. A large drop means the model relied heavily on feature . For additive local explanations, many methods approximate the prediction at a point as , where is a baseline value and each is the contribution attributed to feature .

Worked example

This snippet fits a random forest on Iris data and uses permutation importance to estimate which features the fitted model relies on most.

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
import numpy as np
 
X, y = load_iris(return_X_y=True)
rf = RandomForestClassifier(n_estimators=80, random_state=17).fit(X, y)
r = permutation_importance(rf, X, y, n_repeats=5, random_state=17, n_jobs=1)
print("accuracy", round(rf.score(X, y), 3))
print("permutation_importance_mean", np.round(r.importances_mean, 3))

Observed output:

accuracy 1.0
permutation_importance_mean [0.016 0.011 0.163 0.52 ]

Permuting the fourth Iris feature hurts accuracy most, so this fitted forest relies heavily on it. Because the score is computed on training data here, treat the result as a mechanism demonstration, not a deployment audit.

Caveats

Feature importance is not causality. Correlated features can hide each other’s importance. Explanations should be computed on validation or production-like data, and they should be checked against data leakage because the most “important” feature may be an impossible shortcut.

References