Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Model interpretability and explainability using SHAP (SHapley Additive exPlanations). Use this skill when explaining machine learning model predictions, computing feature importance, generating SHAP plots (waterfall, beeswarm, bar, scatter, force, heatmap), debugging models, analyzing model bias or fairness, comparing models, or implementing explainable AI. Works with tree-based models (XGBoost, LightGBM, Random Forest), deep learning (TensorFlow, PyTorch), linear models, and any black-box model
| Model | Eval pass | Runs |
|---|---|---|
| gemini-3.6-flash | 100% | 5 |
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-10 | ✗→✓ | ▲ Improved | — | — |
| case-13 | ✗→✓ | ▲ Improved | — | — |
| case-16 | ✓→✓ | = Same ✓ | — | — |
| case-01 | ✗→✗ | = Same ✗ | — | — |
| case-21 | ✗→✗ | = Same ✗ | — | — |
SHAP is a unified approach to explain machine learning model outputs using Shapley values from cooperative game theory. This skill provides comprehensive guidance for:
SHAP works with all model types: tree-based models (XGBoost, LightGBM, CatBoost, Random Forest), deep learning models (TensorFlow, PyTorch, Keras), linear models, and black-box models.
Trigger this skill when users ask about:
Decision Tree:
shap.TreeExplainer (fast, exact)shap.DeepExplainer or shap.GradientExplainershap.LinearExplainer (extremely fast)shap.KernelExplainer (model-agnostic but slower)shap.Explainer (automatically selects best algorithm)See references/explainers.md for detailed information on all explainer types.
pythonimport shap # Example with tree-based model (XGBoost) import xgboost as xgb # Train model model = xgb.XGBClassifier().fit(X_train, y_train) # Create explainer explainer = shap.TreeExplainer(model) # Compute SHAP values shap_values = explainer(X_test) # The shap_values object contains: # - values: SHAP values (feature attributions) # - base_values: Expected model output (baseline) # - data: Original feature values
For Global Understanding (entire dataset):
python# Beeswarm plot - shows feature importance with value distributions shap.plots.beeswarm(shap_values, max_display=15) # Bar plot - clean summary of feature importance shap.plots.bar(shap_values)
For Individual Predictions:
python# Waterfall plot - detailed breakdown of single prediction shap.plots.waterfall(shap_values[0]) # Force plot - additive force visualization shap.plots.force(shap_values[0])
For Feature Relationships:
python# Scatter plot - feature-prediction relationship shap.plots.scatter(shap_values[:, "Feature_Name"]) # Colored by another feature to show interactions shap.plots.scatter(shap_values[:, "Age"], color=shap_values[:, "Education"])
See references/plots.md for comprehensive guide on all plot types.
This skill supports several common workflows. Choose the workflow that matches the current task.
Goal: Understand what drives model predictions
Steps:
Example:
python# Step 1-2: Setup explainer = shap.TreeExplainer(model) shap_values = explainer(X_test) # Step 3: Global importance shap.plots.beeswarm(shap_values) # Step 4: Feature relationships shap.plots.scatter(shap_values[:, "Most_Important_Feature"]) # Step 5: Individual explanation shap.plots.waterfall(shap_values[0])
Goal: Identify and fix model issues
Steps:
See references/workflows.md for detailed debugging workflow.
Goal: Use SHAP insights to improve features
Steps:
See references/workflows.md for detailed feature engineering workflow.
Goal: Compare multiple models to select best interpretable option
Steps:
See references/workflows.md for detailed model comparison workflow.
Goal: Detect and analyze model bias across demographic groups
Steps:
See references/workflows.md for detailed fairness analysis workflow.
Goal: Integrate SHAP explanations into production systems
Steps:
See references/workflows.md for detailed production deployment workflow.
Definition: SHAP values quantify each feature's contribution to a prediction, measured as the deviation from the expected model output (baseline).
Properties:
Interpretation:
Example:
Baseline (expected value): 0.30
Feature contributions (SHAP values):
Age: +0.15
Income: +0.10
Education: -0.05
Final prediction: 0.30 + 0.15 + 0.10 - 0.05 = 0.50Purpose: Represents "typical" input to establish baseline expectations
Selection:
Impact: Baseline affects SHAP value magnitudes but not relative importance
Critical Consideration: Understand what your model outputs
Example: XGBoost classifiers explain margin output (log-odds) by default. To explain probabilities, use model_output="probability" in TreeExplainer.
python# 1. Setup explainer = shap.TreeExplainer(model) shap_values = explainer(X_test) # 2. Global importance shap.plots.beeswarm(shap_values) shap.plots.bar(shap_values) # 3. Top feature relationships top_features = X_test.columns[np.abs(shap_values.values).mean(0).argsort()[-5:]] for feature in top_features: shap.plots.scatter(shap_values[:, feature]) # 4. Example predictions for i in range(5): shap.plots.waterfall(shap_values[i])
python# Define cohorts cohort1_mask = X_test['Group'] == 'A' cohort2_mask = X_test['Group'] == 'B' # Compare feature importance shap.plots.bar({ "Group A": shap_values[cohort1_mask], "Group B": shap_values[cohort2_mask] })
python# Find errors errors = model.predict(X_test) != y_test error_indices = np.where(errors)[0] # Explain errors for idx in error_indices[:5]: print(f"Sample {idx}:") shap.plots.waterfall(shap_values[idx]) # Investigate key features shap.plots.scatter(shap_values[:, "Suspicious_Feature"])
Explainer Speed (fastest to slowest):
LinearExplainer - Nearly instantaneousTreeExplainer - Very fastDeepExplainer - Fast for neural networksGradientExplainer - Fast for neural networksKernelExplainer - Slow (use only when necessary)PermutationExplainer - Very slow but accurateFor Large Datasets:
python# Compute SHAP for subset shap_values = explainer(X_test[:1000]) # Or use batching batch_size = 100 all_shap_values = [] for i in range(0, len(X_test), batch_size): batch_shap = explainer(X_test[i:i+batch_size]) all_shap_values.append(batch_shap)
For Visualizations:
python# Sample subset for plots shap.plots.beeswarm(shap_values[:1000]) # Adjust transparency for dense plots shap.plots.scatter(shap_values[:, "Feature"], alpha=0.3)
For Production:
python# Cache explainer import joblib joblib.dump(explainer, 'explainer.pkl') explainer = joblib.load('explainer.pkl') # Pre-compute for batch predictions # Only compute top N features for API responses
Problem: Using KernelExplainer for tree models (slow and unnecessary) Solution: Always use TreeExplainer for tree-based models
Problem: DeepExplainer/KernelExplainer with too few background samples Solution: Use 100-1000 representative samples
Problem: Interpreting log-odds as probabilities Solution: Check model output type; understand whether values are probabilities, log-odds, or raw outputs
Problem: Matplotlib backend issues Solution: Ensure backend is set correctly; use plt.show() if needed
Problem: Default max_display=10 may be too many or too few Solution: Adjust max_display parameter or use feature clustering
Problem: Computing SHAP for very large datasets Solution: Sample subset, use batching, or ensure using specialized explainer (not KernelExplainer)
show=True (default)pythonimport mlflow with mlflow.start_run(): # Train model model = train_model(X_train, y_train) # Compute SHAP explainer = shap.TreeExplainer(model) shap_values = explainer(X_test) # Log plots shap.plots.beeswarm(shap_values, show=False) mlflow.log_figure(plt.gcf(), "shap_beeswarm.png") plt.close() # Log feature importance metrics mean_abs_shap = np.abs(shap_values.values).mean(axis=0) for feature, importance in zip(X_test.columns, mean_abs_shap): mlflow.log_metric(f"shap_{feature}", importance)
pythonclass ExplanationService: def __init__(self, model_path, explainer_path): self.model = joblib.load(model_path) self.explainer = joblib.load(explainer_path) def predict_with_explanation(self, X): prediction = self.model.predict(X) shap_values = self.explainer(X) return { 'prediction': prediction[0], 'base_value': shap_values.base_values[0], 'feature_contributions': dict(zip(X.columns, shap_values.values[0])) }
This skill includes comprehensive reference documentation organized by topic:
Complete guide to all explainer classes:
TreeExplainer - Fast, exact explanations for tree-based modelsDeepExplainer - Deep learning models (TensorFlow, PyTorch)KernelExplainer - Model-agnostic (works with any model)LinearExplainer - Fast explanations for linear modelsGradientExplainer - Gradient-based for neural networksPermutationExplainer - Exact but slow for any modelIncludes: Constructor parameters, methods, supported models, when to use, examples, performance considerations.
Comprehensive visualization guide:
Includes: Parameters, use cases, examples, best practices, plot selection guide.
Detailed workflows and best practices:
Includes: Step-by-step instructions, code examples, decision criteria, troubleshooting.
Theoretical foundations:
Includes: Mathematical foundations, proofs, comparisons, advanced topics.
When to load reference files:
explainers.md when user needs detailed information about specific explainer types or parametersplots.md when user needs detailed visualization guidance or exploring plot optionsworkflows.md when user has complex multi-step tasks (debugging, fairness analysis, production deployment)theory.md when user asks about theoretical foundations, Shapley values, or mathematical detailsDefault approach (without loading references):
Loading references:
python# To load reference files, use the Read tool with appropriate file path: # /path/to/shap/references/explainers.md # /path/to/shap/references/plots.md # /path/to/shap/references/workflows.md # /path/to/shap/references/theory.md
bash# Basic installation uv pip install shap # With visualization dependencies uv pip install shap matplotlib # Latest version uv pip install -U shap
Dependencies: numpy, pandas, scikit-learn, matplotlib, scipy
Optional: xgboost, lightgbm, tensorflow, torch (depending on model types)
This skill provides comprehensive coverage of SHAP for model interpretability across all use cases and model types.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-10 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-13 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-21 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-16 | pass→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-04 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-20 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-22 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-18 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-19 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-09 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-15 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-06 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-12 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-07 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-05 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-03 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-17 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-08 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-02 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-14 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-11 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +9 percentage points is the difference between those two pass rates over the 22 comparable cases.
The per-case answers from this run were removed by the retention sweep, so the case table below shows the verdicts without the text either arm produced. The counts above were recorded at the time and are unaffected. Answers are now kept for 180 days.
Other measured skills in the registry, with their headline benchmark lift.