Sub topic 1
Why Look Inside a Trained Model
Prerequisites
Assume you can train a scikit-learn model, split data into training and test sets, and interpret a metric such as R². This chapter uses Python, NumPy, and basic pandas-style data handling.
Why look inside a trained model
A good score does not explain how a model makes predictions. Model inspection answers questions such as:
- Which features does the model use most?
- Does a feature increase or decrease predictions?
- Does the effect change for different observations?
A model-internal measure uses the model's own structure. A model-agnostic explanation treats the trained model as a black box and studies its inputs and outputs. Permutation importance is global: it summarizes behavior across many rows.
Setting up: a held-out split and a score to track
Use training data to fit the model and held-out test data to inspect generalization. A held-out split is data kept away from fitting. Let the score be R², where larger values are better.
from sklearn.datasets import load_diabetes
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
X, y = load_diabetes(as_frame=True, return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
model = RandomForestRegressor(n_estimators=100, random_state=0)
model.fit(X_train, y_train)
result = permutation_importance(
model, X_test, y_test, n_repeats=10, random_state=0
)Why built-in tree importances are not enough
A tree model's impurity-based importance measures how much splits using a feature reduce impurity during training. It is fast, but it can favor continuous or high-cardinality features. It also describes training behavior, not necessarily predictive value on new data.
Permutation importance: break a feature and watch the score
Permutation importance measures the score drop after randomly shuffling one feature. Shuffling breaks its association with the target while leaving other columns unchanged. A large drop means the model relied on that feature for the chosen data and metric.
| Result | Interpretation |
|---|---|
| Large positive drop | The feature supports predictions |
| Near-zero drop | The model gains little from the feature, or another feature replaces it |
| Negative drop | Shuffling happened to improve the score or reflects sampling noise |
Computing permutation importance step by step
For each feature:
- Compute the baseline score on held-out data.
- Shuffle that feature's values.
- Score the modified data.
- Subtract the shuffled score from the baseline.
- Repeat the shuffle and report the mean and standard deviation.
The standard deviation shows how sensitive the estimate is to the random shuffles. Correlated features can hide importance because another feature may carry similar information.
Exercise
Why should permutation importance usually be computed on held-out data?
Show answer
Held-out data estimates behavior on new cases, while training data can make importance look overly reliable because the model was fitted on it.