SHAP (SHapley Additive exPlanations) › Visualizing SHAP

Michael BrenndoerferJuly 15, 202512 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 51SHAP (SHapley Additive exPlanations) › Visualizing SHAP
Reading section 3 of 8 · Visualizing SHAP · 12 minSection 3 of 8 · 12 min

Book contents

Section 3 of 8 · 12 min read

SHAP (SHapley Additive exPlanations) › Visualizing SHAPOne section · Sections in this chapter3 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The mathematical basis of SHAP gives rigorous attribution values, but these numbers only become meaningful when we can see them in context. Visualization maps the abstract concept of fair feature attribution into concrete insights that guide understanding and decision-making. The key SHAP visualizations each serve different purposes: some reveal overall patterns across the dataset (global explanations), while others focus on individual predictions (local explanations). Together, they make SHAP's formula accessible to practitioners who need to interpret and communicate model behavior.

What makes SHAP visualizations particularly powerful is how they directly reflect the formula's structure: each visualization shows features ordered by their SHAP values (which emerge from the weighted averaging of marginal contributions), making visible the relative importance that the formula calculates. The colors and positions encode the magnitude of contributions and how feature values relate to those contributions, helping us understand both what matters and why it matters in specific contexts.

SHAP Summary Plot: Seeing Global Patterns

The summary plot maps the abstract concept of feature importance into a visual narrative that shows how features influence predictions across the entire dataset. Each point represents a SHAP value for a specific feature-instance pair, the result of applying our formula to that particular prediction. The horizontal position shows the SHAP value magnitude (how much the feature contributed), while the color indicates the feature's actual value, creating a rich view of how different feature values relate to their contributions.

Out[3]:
Visualization
SHAP summary plot with dots showing feature contributions colored by feature values, ordered by importance.
SHAP summary plot showing feature importance and impact. Each point represents a SHAP value for a specific feature and instance. The horizontal position shows the SHAP value (contribution to prediction), while the color indicates the feature value. Features are ordered by importance (average absolute SHAP value). This visualization helps identify which features are most important and how their values influence predictions across the dataset.

How to Interpret the SHAP Summary Plot

Reading a SHAP summary plot requires understanding several visual elements that encode different information about your model:

Y-axis (Vertical Positioning): Features are listed vertically, ordered from top to bottom by their importance, which is calculated as the average absolute SHAP value across all instances. The most important features appear at the top. This ordering shows overall feature importance across your entire dataset, not just for a single prediction.

X-axis (Horizontal Position): Each dot's horizontal position represents the SHAP value for that specific feature-instance pair. The X-axis center (typically zero) represents no contribution. Points to the right indicate positive contributions (the feature increased the prediction), while points to the left indicate negative contributions (the feature decreased the prediction). The farther from center, the larger the contribution magnitude.

Color Encoding: The color of each point represents the actual value of that feature for that specific instance. The color scale typically ranges from blue (low feature values) through white (medium values) to red (high feature values), though the exact mapping depends on the feature's distribution. This color coding lets you see how much a feature contributed and whether high or low values of that feature are associated with positive or negative contributions.

Density and Distribution: The horizontal spread of points for each feature shows the distribution of SHAP values. A wide spread indicates the feature's impact varies significantly across instances. A narrow, concentrated cluster suggests the feature has consistent impact. Points stacked vertically at the same X position represent multiple instances with identical or very similar SHAP values for that feature.

Interpreting Patterns: When reading this plot, look for:

  • Horizontal clustering: Points clustered far from zero indicate features with consistently strong positive or negative impacts
  • Color gradients: If red (high values) consistently appears on the right and blue (low values) on the left, high feature values are associated with increased predictions
  • Overlap: When points from different features overlap horizontally, those features contribute similar amounts to some predictions
  • Outliers: Isolated points far from the main cluster may represent anomalous predictions worth investigating

Practical Reading Strategy: Start by identifying the most important features (top of the plot), then examine how their SHAP values vary (horizontal spread), and finally observe how feature values (colors) relate to contribution direction (left/right position). This view shows which features matter, by how much, and in what direction.

SHAP Waterfall Plot: Decomposing Individual Predictions

Where the summary plot reveals global patterns, the waterfall plot provides a detailed local explanation that makes the efficiency property tangible: it shows exactly how we progress from baseline to final prediction by adding each feature's SHAP contribution. This visualization directly embodies the formula's promise. Starting with the baseline f(∅)f(\emptyset) and cumulatively adding each feature's SHAP value ϕi\phi_i, we arrive exactly at the full prediction f(F)f(F). The waterfall format makes visible the decomposition that the formula guarantees, turning abstract mathematical properties into concrete visual narratives that stakeholders can understand.

Out[4]:
Visualization
SHAP waterfall plot showing cumulative feature contributions from baseline to final prediction.
SHAP waterfall plot for a single prediction, showing how each feature contributes to the final prediction. The plot starts with the baseline (expected value) and shows how each feature pushes the prediction up or down. Features are ordered by their SHAP value magnitude, with the most impactful features shown first. This visualization directly demonstrates the efficiency property: the sum of SHAP values equals the difference between baseline and prediction, making the formula''s mathematical guarantee visually apparent.

How to Interpret the SHAP Bar Plot (Waterfall)

The bar plot provides a focused view of a single prediction, making it ideal for explaining why the model made a specific decision. Understanding its components is essential for interpreting individual predictions:

Y-axis (Feature Names): Features are listed vertically, ordered by the magnitude of their SHAP values for this specific instance, with the largest absolute contributions at the top. This ordering helps you immediately identify which features drove this particular prediction, regardless of whether their contribution was positive or negative.

X-axis (SHAP Values): The horizontal position and length of each bar represents the SHAP value for that feature. The center of the plot (typically zero) represents no contribution. Bars extending to the right show positive contributions (increasing the prediction), while bars extending to the left show negative contributions (decreasing the prediction). The length of the bar directly corresponds to the magnitude of contribution.

Bar Length and Direction: Each bar's length is proportional to the feature's SHAP value magnitude. A long bar to the right means the feature significantly increased the prediction, while a long bar to the left means it significantly decreased it. Short bars indicate minimal contribution. The visual length makes it easy to compare relative contributions at a glance.

Color Coding: Bars are typically colored to reinforce direction: red or warm colors for positive contributions (pushing predictions up) and blue or cool colors for negative contributions (pulling predictions down). Some implementations may use color intensity to indicate magnitude as well.

Feature Ordering Strategy: Since features are ordered by absolute SHAP value, the most impactful features appear at the top, making it easy to scan from top to bottom to understand what drove this specific prediction. Features with similar magnitudes appear near each other, helping you identify which features had comparable influence.

How to Read the Plot: Start at the top and work down, noting the direction (left or right) and length of each bar. Features with long bars are the primary drivers of this prediction. If most bars point in the same direction, the prediction is driven by features with consistent directional influence. Mixed directions indicate a balanced contribution from both positive and negative influences.

The Efficiency Property in Action: If you mentally sum all the SHAP values shown (rightward bars minus leftward bars), you get the total contribution. Adding this to the baseline (expected value) gives you the exact prediction. This visual representation makes the efficiency property tangible, showing how each feature's calculated contribution combines to produce the final output.

Practical Use: This plot excels for explaining individual predictions to stakeholders. You can point to specific features and say "this feature contributed +X units" or "this feature reduced the prediction by Y units," making model decisions transparent and understandable.

SHAP Force Plot: Visualizing Feature Forces

The force plot offers an intuitive metaphor that connects directly to how we think about feature attribution: each feature "forces" the prediction in a particular direction, with the magnitude of the force corresponding to its SHAP value. This visualization makes tangible the idea that features don't just correlate with outcomes. They actively push predictions up or down, and the SHAP formula quantifies exactly how much force each feature exerts. The red/blue color coding and bar lengths provide immediate visual feedback about both the direction and magnitude of each feature's contribution, making it easy to see at a glance which features are driving the prediction and in what direction.

Out[5]:
Visualization
SHAP force plot with baseline and colored bars showing feature contributions pushing prediction up or down.
SHAP force plot showing feature contributions as forces pushing the prediction up or down. The baseline is shown in gray, and each feature's contribution is represented as a colored bar. Red features push the prediction up, while blue features push it down. The length of each bar represents the magnitude of the SHAP value, the result of our formula's weighted averaging of marginal contributions across all contexts. This visualization makes it easy to see which features are most influential and in which direction.

How to Interpret the SHAP Force Plot

The force plot visualizes feature contributions as physical forces acting on a prediction scale, making the efficiency property immediately visible. Reading it correctly requires understanding its unique layout:

The Prediction Scale: The horizontal axis represents a continuous prediction scale, typically showing the baseline (expected value) as a reference point, often marked or colored distinctly. The final prediction appears at the rightmost point of the plot, showing where all the forces push the prediction.

Baseline Reference: The baseline, which is the expected prediction value (f(∅)f(\emptyset)), is the starting point. It's usually shown as a distinct marker or reference line, representing what the model would predict with no feature information.

Force Bars (Feature Contributions): Each feature appears as a colored bar that "pushes" the prediction from the baseline toward the final value. The bars are arranged sequentially along the prediction scale, showing how each feature moves the prediction.

Bar Direction and Color: Red bars (or warm colors) push the prediction higher (to the right), while blue bars (or cool colors) push it lower (to the left). The color intensity may also reflect magnitude, with brighter colors indicating larger contributions. This color coding provides immediate visual feedback about whether each feature increases or decreases the prediction.

Bar Length and Magnitude: The length of each bar represents the absolute magnitude of the SHAP value. Longer bars indicate larger contributions, making it easy to compare which features had the most impact. The bars are typically arranged to show the cumulative effect, with each bar starting where the previous one ended, creating a cascading effect.

Feature Ordering: Features are usually ordered by their SHAP value magnitude, with the most impactful features shown first. However, the ordering may vary depending on implementation, with some plots ordering by absolute value and others maintaining a different sequence.

Reading the Flow: To interpret the plot, start from the baseline (left) and follow the colored bars as they push the prediction toward the final value (right). Each bar shows magnitude and direction: features that push the prediction up appear above or to the right of the baseline line, while those pushing it down appear below or to the left.

Feature Labels: Each bar is typically labeled with the feature name and may include the actual feature value or SHAP value. These labels help you connect the visual representation to specific features in your dataset.

The Cumulative Effect: The plot visually demonstrates the efficiency property: if you mentally trace from baseline through all the force bars, you arrive at the final prediction. The sum of all positive pushes minus all negative pushes equals the distance from baseline to final prediction, making the complete decomposition visible.

Pattern Recognition: Look for dominance (one or two very long bars indicating a few features driving the prediction), balance (many moderate bars suggesting distributed influence), or cancellation (long bars in opposite directions indicating features with competing effects that partially offset each other).

Practical Interpretation: This plot is particularly powerful for understanding the "journey" from baseline to prediction. It shows which features matter and how they combine sequentially to produce the final result. When explaining to stakeholders, you can literally point to the plot and say "these features pushed the prediction up, these pushed it down, and together they resulted in this final value."