SHAP (SHapley Additive exPlanations) › Implementation: Bringing Theory to Practice

Michael BrenndoerferJuly 15, 202514 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 51SHAP (SHapley Additive exPlanations) › Implementation: Bringing Theory to Practice
Reading section 5 of 8 · Implementation: Bringing Theory to Practice · 14 minSection 5 of 8 · 14 min

Book contents

Section 5 of 8 · 14 min read

SHAP (SHapley Additive exPlanations) › Implementation: Bringing Theory to PracticeOne section · Sections in this chapter5 / 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

Having worked through the mathematical foundation and a manual calculation, we're now ready to see how SHAP translates into practical Python code. The SHAP library provides optimized implementations that handle the computational complexity we discussed. For models with many features, it uses intelligent approximations that maintain the mathematical guarantees while being computationally tractable.

The library's architecture reflects the universality we emphasized earlier: different model types get specialized explainers (TreeExplainer for tree-based models, LinearExplainer for linear models, etc.) that exploit model-specific structure for efficiency, but all produce SHAP values that satisfy the same mathematical properties. This means you can use the same explanation framework across your entire machine learning pipeline while getting optimal performance for each model type.

What makes the implementation useful is how it makes the formula's guarantees visible: when you compute SHAP values, you can verify the efficiency property programmatically. This built-in verification connects directly to the mathematical basis.

In[6]:
Code
import warnings

warnings.filterwarnings("ignore")

from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split

# Generate sample dataset
X, y = make_regression(n_samples=200, n_features=4, noise=0.1, random_state=42)
feature_names = ["Feature A", "Feature B", "Feature C", "Feature D"]

# Split the data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Train a random forest model
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

print(f"Model R² score: {model.score(X_test, y_test):.3f}")
Out[7]:
Console
SHAP values shape: (40, 4)
Expected value (baseline): -3.244
Sum of SHAP values for first instance: -82.750
Actual prediction for first instance: -85.994
Baseline + SHAP sum: -85.994

Notice how the code directly verifies the efficiency property: the baseline plus the sum of SHAP values equals the actual prediction. This programmatic check makes visible the mathematical guarantee we've been discussing, the complete decomposition that makes SHAP explanations trustworthy and interpretable.

SHAP Summary Plot: Visualizing Global Patterns

Out[8]:
Visualization
SHAP summary plot showing feature importance with points colored by feature values across test dataset.
SHAP summary plot for the test dataset. Each point is a SHAP value for one feature and one instance. Features are ordered by average absolute SHAP value, and color indicates the feature value.

Interpreting This Summary Plot

This summary plot uses the same structure as the earlier example, but now you're seeing it applied to your actual test data. Here's how to read it:

Feature Importance Ranking: The vertical ordering shows which features matter most across your test set. Features at the top have the highest average absolute SHAP values, meaning they consistently have large impacts on predictions. This global ranking helps you understand overall model behavior, not just individual predictions.

Contribution Patterns: The horizontal distribution of points reveals how each feature typically contributes. If points cluster far to the right, that feature consistently increases predictions. If they cluster far to the left, it consistently decreases them. A symmetric distribution around zero indicates the feature has variable impact depending on context.

Value-Contribution Relationships: The color coding (blue to red representing low to high feature values) reveals whether high or low values of a feature are associated with increased predictions. If red points cluster on the right and blue on the left for a feature, high values of that feature increase predictions and low values decrease them. The reverse pattern indicates an inverse relationship.

Instance-Level Insights: Each point represents one test instance. Densely populated areas indicate many instances with similar SHAP values for that feature. Sparse regions or outliers may represent unusual cases worth investigating, potentially revealing data quality issues or model limitations.

Comparative Analysis: You can compare features by examining their relative vertical positions (importance) and horizontal spreads (consistency of impact). Features with similar positions and spreads have comparable importance and variability. Large differences suggest some features dominate while others have minimal or variable influence.

SHAP Waterfall Plot

Out[9]:
Visualization
SHAP waterfall plot showing cumulative feature contributions for a test instance.
SHAP waterfall plot for the first test instance, 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. This visualization is excellent for explaining individual predictions to stakeholders.

Interpreting This Waterfall Plot for Test Instance 0

This bar plot shows exactly how the model reached its prediction for the first test instance. Here's how to read it systematically:

Reading from Top to Bottom: The features are ordered by absolute SHAP value magnitude, so the most impactful features for this specific prediction appear first. Start reading from the top to identify what primarily drove this prediction.

Bar Direction Tells the Story: Each bar's direction (left for negative, right for positive) shows whether that feature increased or decreased the prediction for this instance. Long bars indicate dominant influences. If you see several long bars all pointing in the same direction, those features worked together to push the prediction in that direction.

Magnitude Comparison: The relative bar lengths let you compare contributions. If one bar is twice as long as another, it contributed roughly twice as much. This visual comparison makes it easy to identify which features were most critical for this specific instance.

Context-Specific Attribution: This plot shows contributions for one specific instance. A feature that appears large here might be small for other instances, because SHAP values are context-dependent. The ordering is local to this instance, not global.

Efficiency Verification: You can verify the efficiency property by mentally summing the bar lengths (rightward bars as positive, leftward bars as negative). This sum should equal the difference between the baseline and the actual prediction for this instance, confirming that all contributions are accounted for.

Practical Interpretation: Use this plot to answer "Why did the model predict this value for this instance?" The top few bars explain the primary drivers. You can explain to stakeholders: "For this prediction, Feature A increased the prediction by X units, Feature B decreased it by Y units, and so on."

SHAP Force Plot

Out[10]:
Visualization
SHAP force plot with baseline and colored bars showing feature contributions.
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. This visualization makes it easy to see which features are most influential and in which direction.

Interpreting This Force Plot for Test Instance 0

This force plot shows the same prediction as the bar plot above, but in a format that emphasizes the sequential "journey" from baseline to final prediction. Here's how to read it:

Baseline as Starting Point: Identify the baseline marker (typically shown as f(∅)f(\emptyset) or the expected value). This represents what the model would predict with no feature information. All forces act relative to this starting point.

Tracing the Prediction Path: Follow the colored bars from left (baseline) to right (final prediction). Each bar represents a feature "force" that pushes the prediction along the scale. The cumulative effect of all bars moves the prediction from baseline to its final value.

Force Magnitude and Direction: Bar length indicates contribution magnitude, while color (red/blue) indicates direction. Red bars push right (increase prediction), blue bars push left (decrease prediction). The longest bars represent the strongest forces driving this prediction.

Sequential Impact Visualization: Unlike the bar plot which shows unordered contributions, this plot shows how features combine sequentially. The ordering helps you understand the relative importance and how each feature moves the prediction along its path.

The Final Prediction Mark: The rightmost point shows where all forces push the prediction. The distance from baseline to this final point equals the sum of all SHAP values, visually demonstrating the efficiency property.

Pattern Analysis: Look for patterns in the force bars. Do they mostly push in one direction (indicating a consistent directional influence), or do they alternate (suggesting competing forces)? Large blue bars followed by large red bars (or vice versa) indicate features with offsetting effects.

Feature Values and Labels: Each bar is typically labeled with the feature name and its actual value for this instance. This lets you connect the abstract "force" concept to concrete feature values: "When Feature A has value X, it exerts this force."

Stakeholder Communication: This visualization is powerful for explaining predictions because you can literally trace with your finger from baseline to final value, saying "these features pushed it up, these pushed it down, and here's where we ended up." The physical metaphor makes the abstract mathematical concept tangible.

SHAP Bar Plot

Out[11]:
Visualization
SHAP bar plot showing feature importance ranking by mean absolute SHAP values.
SHAP bar plot showing the mean absolute SHAP values for each feature. This is one of the most straightforward ways to understand which features are most important for the model's predictions.

Interpreting the Mean Absolute SHAP Bar Plot

This bar plot provides a simplified, aggregated view of feature importance by showing the mean absolute SHAP value for each feature. It's one of the most straightforward SHAP visualizations:

Y-axis (Feature Ordering): Features are listed vertically, ordered from top to bottom by their mean absolute SHAP value across all instances in the test set. The feature at the top has the highest average absolute contribution. This ordering shows global importance across your entire dataset.

X-axis (Mean Absolute SHAP Values): The horizontal length of each bar represents the mean absolute SHAP value for that feature. This is calculated by taking the absolute value of SHAP values for each instance, then averaging across all instances. Longer bars indicate features that consistently have larger impacts, regardless of whether those impacts are positive or negative.

Bar Length as Importance Metric: Unlike other plots that show direction (positive/negative), this plot focuses purely on magnitude. A feature with a long bar consistently has large impacts on predictions, while a short bar indicates minimal average impact. This makes it easy to rank features by importance.

Why Mean Absolute Values: By taking absolute values before averaging, this plot treats positive and negative contributions equally. A feature that sometimes adds +10 and sometimes adds -10 will show the same importance as one that always adds +10. This is useful when you care about total impact rather than directional influence.

Comparison Across Features: The relative bar lengths let you easily compare feature importance. If one bar is twice as long as another, that feature has roughly twice the average impact. This visual comparison helps prioritize which features to focus on for model understanding or feature engineering.

Limitations to Remember: This plot doesn't show direction of influence (positive vs. negative) or variability. A feature with consistent small contributions might rank similarly to one with highly variable large contributions. For direction and variability, refer back to the summary plot with individual points.

Practical Use Cases: This plot excels when you need to quickly answer "What are the most important features overall?" It's ideal for stakeholder presentations, feature selection discussions, and model documentation where a simple importance ranking is needed.

Combining with Other Plots: Use this plot alongside the detailed summary plot. This bar plot tells you "which features matter most," while the summary plot shows "how they matter" (direction, variability, value relationships). Together, they show how the features affect the model.

Alternative Implementation: Linear Model

For linear models, SHAP provides even more efficient computation since the SHAP values can be computed directly from the model coefficients:

In[12]:
Code
from sklearn.linear_model import LinearRegression

# Train a linear regression model
linear_model = LinearRegression()
linear_model.fit(X_train, y_train)

# Create SHAP explainer for linear models
linear_explainer = shap.LinearExplainer(linear_model, X_train)
linear_shap_values = linear_explainer.shap_values(X_test)

print(f"Linear model R² score: {linear_model.score(X_test, y_test):.3f}")
print(f"Linear SHAP values shape: {linear_shap_values.shape}")
Out[13]:
Visualization
SHAP summary plot showing feature importance for linear regression model.
SHAP summary plot for linear regression model, showing feature importance and impact. Linear models have the advantage that SHAP values can be computed exactly and efficiently, making them particularly suitable for interpretable machine learning applications.

Interpreting the Linear Model SHAP Summary Plot

This summary plot for the linear regression model uses the same structure as previous summary plots, but interpreting it in the context of linear models reveals important insights:

Linear Model Characteristics: For linear models, SHAP values have a direct relationship to model coefficients, but they account for feature interactions in the context. The plot shows how these contextualized contributions vary across instances, even though the underlying model is linear.

Feature Ordering by Importance: The vertical ordering reflects average absolute SHAP values across test instances. In linear models, this ordering often correlates with coefficient magnitudes, but not always, because SHAP values incorporate contextual effects. Features with large coefficients typically appear near the top, but the exact order depends on how features interact with each other in practice.

Contribution Distribution Patterns: The horizontal spread of points reveals how each feature's contribution varies. In ideal linear models with independent features, you might see narrow, consistent distributions. Wider spreads indicate that features have variable impact depending on other feature values, even in a linear model.

Color-Feature Value Relationships: The color coding (blue for low values, red for high values) shows how feature values relate to contributions. For linear models with positive coefficients, you typically see red points (high values) on the right (positive contributions) and blue points (low values) on the left (negative or small contributions), creating a gradient that reflects the linear relationship.

Symmetry and Linearity: Linear models often produce more symmetric distributions than complex models. If the model is truly linear with independent features, points for each feature might cluster tightly around a central value. Asymmetry or wide spreads suggest that interactions or correlations between features are affecting contributions, even within the linear framework.

Coefficient Validation: You can use this plot to validate that SHAP values align with model coefficients. For a feature with a large positive coefficient, most points should appear on the right (positive SHAP values). However, SHAP values will vary based on instance-specific contexts, so some variability is expected and normal.

Comparing to Random Forest: If you compare this linear model plot to the earlier random forest plot, you might notice differences in:

  • Distribution widths (linear models often have narrower spreads)
  • Point clustering patterns (linear relationships produce different clustering than tree-based decisions)
  • Feature ordering (which features rank as most important can differ between model types)

Exact vs. Approximate Computation: Linear models allow exact SHAP value computation (no approximation needed), so every point in this plot represents a precise calculation. This precision is visible in potentially cleaner, more consistent patterns compared to approximated SHAP values from complex models.

Practical Insights: Use this plot to understand how your linear model behaves across different instances. Even though the model structure is simple, SHAP reveals how feature contributions vary with context. This helps identify whether assumptions of linearity and independence hold in practice, and guides decisions about model complexity.