Explainer is a SHAP class that computes feature-attribution explanations for predictions or scores produced by a model. For anomaly detection, it helps identify which features contribute to global score patterns and to an individual record’s score. section 13.2.1
Use Explainer to:
- Create an explanation object by passing a fitted detector to shap.Explainer and evaluating it on the original feature columns. section 13.2.1
- Inspect global importance with bar plots, or examine broader value-direction patterns with beeswarm plots. section 13.2.1
- Inspect a local explanation for one scored record, especially the record ranked most anomalous. section 13.2.1
Isolation Forest on abalone data
Sort records by their Isolation Forest score, construct shap.Explainer(det), and evaluate the original features. Global plots identify influential features, while a local bar plot explains the highest-scored record; categorical encoding can expose the importance of individual values. section 13.2.1
Listing 13.2 Determining the SHAP global feature importances
import shap
df = df.sort_values('IF Score') #1
explainer = shap.Explainer(det) #2
explanation = explainer(df[orig_features])
The concise implementation shows detector handoff, feature selection, and explanation-object creation. section 13.2.1
