Sub topic 17
Interpreting models with importances and SHAP
This chapter assumes you can train a LightGBM or CatBoost model with a validation set, as in the earlier chapters. It covers two ways to ask what the model learned: built-in feature importances and SHAP values.
Built-in importances
Both libraries report importance per feature. The number depends on how it is computed.
| Type | Meaning | Common bias |
|---|---|---|
| Split count | How often a feature is used in a split | Favors high-cardinality and continuous features |
| Gain | Total loss reduction from splits on the feature | Favors features used near the root of many trees |
| Permutation | Score drop when the feature is shuffled | Splits credit among correlated features |
In LightGBM, model.feature_importances_ returns split counts by default. Set importance_type="gain" for gain. CatBoost's get_feature_importance() uses PredictionValuesChange by default, which measures how much predictions change on average when the feature value changes.
import pandas as pd
import lightgbm as lgb
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.2, random_state=0
)
model = lgb.LGBMRegressor(
n_estimators=300, learning_rate=0.05, importance_type="gain", verbose=-1
)
model.fit(X_train, y_train)
importance = pd.Series(model.feature_importances_, index=X_train.columns)
print(importance.sort_values(ascending=False))SHAP values
SHAP assigns each feature a contribution to each individual prediction. For a row, the contributions plus a base value (the average prediction) add up to the model output. Positive values push the prediction up and negative values push it down.
shap.TreeExplainer computes these values exactly and efficiently for tree ensembles, including LightGBM and CatBoost.
import shap
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_valid)
shap.summary_plot(shap_values, X_valid)In the summary plot, each dot is one row. Position on the horizontal axis is the SHAP value, and color is the feature value. Features are sorted by mean absolute SHAP value, which gives a global importance that is consistent with the per-row explanations.
Common errors
- Explaining the training set only. Compute SHAP values on validation data to see what drives held-out predictions.
- Reading SHAP as causation. A large value means the model relies on the feature. It does not mean changing the feature changes the real outcome.
- Shape surprises in classifiers. For binary LightGBM models, older
shapversions return a list of two arrays. Newer versions return one array. Checktypeandshapebefore plotting. - Slow runs on large data. Compute SHAP on a sample of a few thousand rows.
Exercise
A feature has the highest split-count importance in your LightGBM model, but permutation importance on the validation set is near zero. What are two likely explanations?
Show answer
The feature may be continuous or high-cardinality, so it is picked for many splits without adding much accuracy, which split count rewards. It may also be overfit to the training data, so shuffling it on validation data barely changes the score. A correlated feature could carry the same information as well.