Once you have calculated the SHAP values for a specific prediction using methods like KernelSHAP or TreeSHAP, you need a way to understand what they mean. How do individual feature values push the model's output away from the baseline for that particular instance? This is where SHAP visualizations come in, and the force plot is a primary tool for visualizing these local explanations.
Think of a force plot as a visual representation of a tug-of-war. On one side, you have the base value. This represents the average prediction the model would make if it didn't have any specific information about the features for the instance you're examining. It's essentially the average model output over the background dataset (often the training set).
On the other side is the final prediction (output value) for the specific instance. Between these two points, the features act as forces, pushing the prediction either higher or lower.
Here's how it works:
The core idea is that the sum of the base value and all the individual feature SHAP values equals the final output value for that prediction. Mathematically:
Model Output=Base Value+∑SHAP values for each feature
This additive property, inherited from Shapley values, makes force plots intuitive. They show exactly how the model arrived at a specific prediction, starting from a baseline and adding or subtracting the contributions of each feature.
Interpreting a force plot involves looking at these components:
Imagine you've trained a model to predict house prices, and you want to explain its prediction for a specific house. Let's say the average predicted price (base value) across your dataset is 250k.Foroneparticularhouse,themodelpredicts350k. A force plot might reveal:
sqft_living = 2000
: Pushes the price up significantly (large red segment, e.g., +$80k SHAP value).grade = 8
: Pushes the price up (medium red segment, e.g., +$40k SHAP value).yr_built = 1950
: Pulls the price down slightly (small blue segment, e.g., -$15k SHAP value).zipcode = 98103
: Pulls the price down minimally (very small blue segment, e.g., -$5k SHAP value).The force plot visually arranges these, showing how the large positive contributions from sqft_living
and grade
outweigh the smaller negative contributions from yr_built
and zipcode
, resulting in a final prediction higher than the base value.
While the original shap
library generates interactive JavaScript force plots, we can represent the core concept using other chart types. The following waterfall chart illustrates the additive nature:
A waterfall chart illustrating the concept behind a SHAP force plot. Starting from the base value (average prediction), positive SHAP values (red) increase the prediction, while negative SHAP values (blue) decrease it, leading to the final prediction for this specific instance.
The shap
library makes generating force plots straightforward. Assuming you have already calculated an explainer
and shap_values
(as discussed in previous sections), and you have your feature data X
, you can plot the explanation for a single instance (e.g., the one at index i
):
import shap
# Assume 'explainer' is a fitted SHAP explainer (e.g., KernelExplainer, TreeExplainer)
# Assume 'shap_values' is the array/list of SHAP values for your dataset
# Assume 'X' is your DataFrame or NumPy array of features
# Choose the index of the instance to explain
instance_index = 0
# Load JavaScript visualization library (needed for Jupyter notebooks, etc.)
shap.initjs()
# Generate the force plot for the chosen instance
shap.force_plot(
explainer.expected_value, # The base value
shap_values[instance_index], # SHAP values for this instance
X.iloc[instance_index] # Feature values for this instance
)
This code will typically render an interactive plot in environments like Jupyter notebooks. The plot visually confirms the additive relationship: explainer.expected_value + shap_values[instance_index].sum()
should be very close to the model's prediction for X.iloc[instance_index]
.
Force plots are primarily for local explanation (one prediction at a time). However, the shap
library allows stacking multiple force plots together, often rotated vertically. This can provide a semi-global view by showing explanations for many samples simultaneously. Features that consistently push predictions in the same direction across many samples become apparent.
# Generate force plot for multiple instances (e.g., the first 100)
# Note: This can be computationally intensive for large datasets/plots
shap.force_plot(
explainer.expected_value,
shap_values[:100], # SHAP values for the first 100 instances
X.iloc[:100] # Feature values for the first 100 instances
)
This stacked view helps identify broader patterns while still being based on individual instance explanations.
Force plots offer a powerful and theoretically grounded way to peer inside model predictions one instance at a time. They directly visualize the impact of each feature, making it clear why a specific prediction was made relative to the baseline, which is essential for debugging, validating, and building trust in your models.
© 2025 ApX Machine Learning