0Pricing
Learn AI with Python · Lesson

SHAP Values for Model Explainability

shap.Explainer, TreeExplainer, force plots, beeswarm plots, feature importance ranking.

SHAP Values for Model Explainability is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Explainability

Complex models are accurate but opaque. Explainability answers "why did the model make this prediction?", which is essential for trust, debugging, and regulatory compliance. SHAP is one of the most widely used explanation methods.

What SHAP Measures

SHAP (SHapley Additive exPlanations) assigns each feature a contribution to a single prediction. The values are based on Shapley values from cooperative game theory: each feature is a "player" and its SHAP value is its fair share of the prediction relative to a baseline.

Additivity

SHAP values are additive: the baseline (average model output) plus the sum of all feature SHAP values equals the actual prediction. This makes every explanation a complete, auditable accounting of the output.

# prediction = base_value + sum(shap_values_for_each_feature)

TreeExplainer

For tree models (XGBoost, LightGBM, random forests) shap.TreeExplainer computes exact SHAP values fast by exploiting tree structure.

import shap
import xgboost

model = xgboost.XGBClassifier().fit(X_train, y_train)
explainer = shap.TreeExplainer(model)

Computing shap_values

explainer.shap_values(X_test) returns a matrix: one SHAP value per feature per row. These are the raw contributions you will visualize.

shap_values = explainer.shap_values(X_test)
print(shap_values.shape)  # (n_samples, n_features)

The summary_plot Beeswarm

shap.summary_plot produces a beeswarm: each dot is one sample for one feature. Position shows the SHAP value (impact direction and magnitude); color shows the feature value. Features are ranked by overall importance.

shap.summary_plot(shap_values, X_test)

Reading the Beeswarm

To read a beeswarm:

  • Top features matter most globally
  • Dots far right push the prediction up; far left push it down
  • Color reveals direction, e.g. high feature values (red) on the right means "high value increases the prediction"

Explaining One Prediction

SHAP shines at the individual level. To explain a single row you need its SHAP values and the model base value.

idx = 0
base = explainer.expected_value
row_shap = shap_values[idx]

force_plot

shap.force_plot shows one prediction as forces pushing from the base value: red features push the output higher, blue features push it lower. The net result lands on the final prediction.

shap.force_plot(
    explainer.expected_value,
    shap_values[0],
    X_test.iloc[0],
)

waterfall_plot

shap.waterfall_plot is the most readable single-prediction view. It starts at the base value and stacks each feature contribution step by step until it reaches the final output, sorted by magnitude.

shap.waterfall_plot(
    shap.Explanation(
        values=shap_values[0],
        base_values=explainer.expected_value,
        data=X_test.iloc[0],
    )
)

Global vs Local

SHAP serves two needs:

  • Global: summary_plot shows which features drive the model overall
  • Local: force_plot and waterfall_plot explain one specific prediction

Both come from the same SHAP values, giving a consistent story across scales.

Quick Check

Test your SHAP knowledge.

Recap

You learned model explainability with SHAP:

  • shap.TreeExplainer(model) computes exact values for tree models
  • explainer.shap_values(X_test) returns per-feature contributions
  • summary_plot beeswarm shows global importance
  • force_plot and waterfall_plot explain a single prediction
  • SHAP values are additive: base + contributions = prediction

Frequently asked questions

Is the “SHAP Values for Model Explainability” lesson free?

Yes — the full text of “SHAP Values for Model Explainability” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “SHAP Values for Model Explainability”?

shap.Explainer, TreeExplainer, force plots, beeswarm plots, feature importance ranking. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “SHAP Values for Model Explainability” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Bias Detection in ML Models
  2. SHAP Values for Model Explainability
  3. LIME: Local Interpretable Explanations
  4. AI Ethics and Governance Frameworks
← Back to Learn AI with Python