Sub topic 14
Inspecting which features the model uses
This chapter assumes you have the fitted model, the chronological test split and the feature_cols list from the earlier chapters. It also assumes you know the basics of scikit-learn's fit/predict interface.
Why inspect the model
A gradient boosting model is hard to read directly. Checking which features it relies on helps you in three ways. It confirms the model uses the signal you expect, such as lag_7 for weekly data. It shows which features you can drop. It can reveal leakage, because a feature that is suspiciously dominant often contains information from the future.
Permutation importance
HistGradientBoostingRegressor has no feature_importances_ attribute. Use permutation importance instead. It shuffles one column at a time and measures how much the error rises. A large rise means the model depends on that column.
from sklearn.inspection import permutation_importance
result = permutation_importance(
model,
test[feature_cols],
test['y'],
scoring='neg_mean_absolute_error',
n_repeats=10,
random_state=0,
)
order = result.importances_mean.argsort()[::-1]
for i in order:
print(f"{feature_cols[i]:12s} {result.importances_mean[i]:8.3f} "
f"+/- {result.importances_std[i]:.3f}")Each value is the average increase in MAE when that column is shuffled. The +/- value shows how much the result varies across the 10 shuffles.
Run this on the test split, not the training split. On training data the model can rely on features that only help it memorize noise, and permutation importance will rate them as useful.
Reading the result
| Observation | Likely meaning |
|---|---|
| `lag_1` ranks first | Normal for smooth series. The latest value is the best single predictor. |
| `lag_7` or `dayofweek` ranks high | The model has found weekly seasonality. |
| A feature has importance near zero | The model does not need it. It is a candidate to remove. |
| One feature dwarfs all others | Check for leakage before you celebrate. |
| Importance is negative | Shuffling helped slightly, so the feature is noise. |
What importance does not tell you
Permutation importance says how much the model uses a feature. It does not say in which direction the feature pushes the forecast, or whether the relationship is causal. For direction, plot partial dependence with sklearn.inspection.PartialDependenceDisplay. For single-prediction explanations, the shap library works with tree models.
Importance also describes one fitted model on one test period. If the series changes behavior, the ranking can change. Compute it on a few walk-forward folds and check that the order is stable.
Exercise
Your permutation importance shows that a feature built with s.rolling(7).mean() has far higher importance than every lag feature. The rolling mean was computed without a prior shift(1). What is the most likely problem, and how do you fix it?
Show answer
The window includes the current value, so the feature leaks the target. Compute the rolling statistic on s.shift(1), as make_features does, then rebuild the table, refit the model and recompute importance.