Ethical AI · Intermediate level

Intermediate level · Module 6: Introduction to Explainable AI · Lesson 3 of 3

Local Explanations with SHAP

Learners use SHAP to explain an individual student's prediction as contributions from each feature, read a waterfall plot and translate it into a plain-language explanation.

In this lesson you will

  • Compute SHAP values for one prediction
  • Translate a SHAP explanation into plain language

Key concepts

Shapley valuesbase valuefeature contributionLinearExplainer / Explainerwaterfall plot

The lesson

SHAP explains a prediction as a starting point (the base value, normally the average prediction over a background dataset) plus a contribution from each feature. The contributions add up exactly to the model's prediction for that case.

For linear models, shap.LinearExplainer is fast and exact. shap.Explainer chooses a suitable method for many model types, and shap.TreeExplainer is used for tree ensembles. shap.plots.waterfall shows one prediction's contributions as a waterfall chart.

A SHAP explanation must be translated for the person affected: 'Your predicted score is lower than average mainly because of low attendance (about 11 points) and missed homework (about 4 points).' It describes the model, not the person, and it does not say what caused the student's results in reality.

Step by step

  1. Install SHAP or use the offline wheel, and create an explainer with the training data as background.
  2. Compute the explanation for one test student.
  3. Plot it with shap.plots.waterfall.
  4. Write a two-sentence plain-language explanation and one caution about what it does not show.
Code example

Explaining one student's predicted score with SHAP

A linear model predicts a final score, and SHAP splits one student's prediction into an average starting point plus a contribution from each input.

Pythonexplain_student.py
import numpy as npimport pandas as pdimport shapfrom sklearn.linear_model import LinearRegression # Fictional records: attendance (%), homework completed (out of 10), distance to school (km)X = pd.DataFrame({    "attendance":  [95, 88, 70, 60, 92, 75, 55, 82],    "homework":    [9, 7, 5, 4, 8, 6, 3, 7],    "distance_km": [1, 3, 8, 12, 2, 6, 10, 4],})y = [78, 70, 52, 45, 74, 58, 40, 66]   # final examination score model = LinearRegression().fit(X, y)explainer = shap.LinearExplainer(model, X)   # background data = the training recordsexplanation = explainer(X.iloc[[3]])          # the fourth student print("Average prediction:", round(float(np.ravel(explanation.base_values)[0]), 1))for name, value in zip(X.columns, explanation.values[0]):    print(f"{name:12s} {value:+.1f}")print("This student's prediction:", round(float(model.predict(X.iloc[[3]])[0]), 1)) shap.plots.waterfall(explanation[0])   # draws the same breakdown as a chart

Output

Average prediction: 60.4
attendance   -11.1
homework     -3.9
distance_km  -1.0
This student's prediction: 44.4

How it works

SHAP starts from the model's average prediction over the background data (60.4) and adds one contribution per input. For a linear model with independent background data, each contribution is the coefficient multiplied by the difference between the student's value and the average value. This student's attendance (60% against an average of about 77%) lowers the prediction by about 11 points, homework (4 against about 6) by about 4, and distance by about 1. The contributions add up exactly: 60.4 - 11.1 - 3.9 - 1.0 = 44.4. In plain language: 'Your predicted score is below average mainly because of low attendance and missed homework.' The explanation describes the model, not the causes of the student's results. With only eight records, and with distance closely related to attendance, the individual coefficients are unstable; a real model needs far more data.

Key concepts

base valueadditive feature contributionsLinearExplainerwaterfall plotexplaining the model, not the person

Practice activity

Explain three individual predictions from the project model with SHAP and write a plain-language version of each.

Expected result: Three correct SHAP explanations with accurate, careful plain-language summaries.

Check your understanding

  1. What do SHAP contributions add up to?

    Show answer

    The model's prediction minus the base value.

  2. Does a SHAP explanation say what caused a student's real results?

    Show answer

    No. It explains the model's prediction, not real-world causes.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.