scatter plot

This notebook is designed to demonstrate (and so document) how to use the shap.plots.scatter function. It uses an XGBoost model trained on the classic UCI adult income dataset (which is a classification task to predict if people made over \$50k in the 90s).

[2]:
import xgboost
import shap

# train XGBoost model
X,y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)

# compute SHAP values
explainer = shap.Explainer(model, X)
shap_values = explainer(X[:1000])

Simple dependence scatter plot

A dependence scatter plot shows the effect a single feature has on the predictions made by the model. In this example the log-odds of making over 50k increases significantly between age 20 and 40.

  • Each dot is a single prediction (row) from the dataset.

  • The x-axis is the value of the feature (from the X matrix, stored in shap_values.data).

  • The y-axis is the SHAP value for that feature (stored in shap_values.values), which represents how much knowing that feature’s value changes the output of the model for that sample’s prediction. For this model the units are log-odds of making over 50k annually.

  • The light grey area at the bottom of the plot is a histogram showing the distribution of data values.

[4]:
# Note that we are slicing off the column of the shap_values Explanation corresponding to the "Age" feature
shap.plots.scatter(shap_values[:,"Age"])
../../../_images/example_notebooks_api_examples_plots_scatter_3_0.png

Using color to highlight interaction effects

The vertical dispersion in the plot above shows that the same value for the Age feature can have a different impact on the model’s output for different people. This means there are non-linear interaction effects in the model between Age and other features (otherwise the scatter plot would perfectly follow the line given by shap.plots.partial_dependence).

To show which feature may be driving these interaction effects we can color our Age dependence scatter plot by another feature. If we pass the entire Explanation object to the color parameter then the scatter plot attempts to pick out the feature column with the strongest interaction with Age. If an interaction effect is present between this other feature and the feature we are plotting it will show up as a distinct vertical pattern of coloring. For the example below, 20-year-olds with a high level of education are less likely make over \$50k than 20-year-olds with a low level of education. This suggests an interaction effect between Education-Num and Age.

[5]:
shap.plots.scatter(shap_values[:, "Age"], color=shap_values)
../../../_images/example_notebooks_api_examples_plots_scatter_5_0.png

To explicitly control which feature is used for coloring you can pass a specific feature column to the color parameter.

[6]:
shap.plots.scatter(shap_values[:, "Age"], color=shap_values[:,"Workclass"])
../../../_images/example_notebooks_api_examples_plots_scatter_7_0.png

In the plot above we see that the Workclass feature is encoded with a number for the sake of the XGBoost model. When plotting though we often would rather use the original string values before they were categorically encoded. To do this we can set the .display_data property of the Explanation object to a parallel version of the data we would like displayed in plots.

[7]:
X_display,y = shap.datasets.adult(display=True)
shap_values.display_data = X_display.values

shap.plots.scatter(shap_values[:, "Age"], color=shap_values[:,"Workclass"])
../../../_images/example_notebooks_api_examples_plots_scatter_9_0.png

Using global feature importance orderings

Sometimes we don’t know the name or index of the feature we want to plot, we just want to plot the most important features. To do that we can use the dot-chaining capability of the Explanation object to compute a measure of global feature importance, sort by that measure (descending), and then pick out the top feature (which in this case is Age):

[10]:
shap.plots.scatter(shap_values[:, shap_values.abs.mean(0).argsort[-1]])
../../../_images/example_notebooks_api_examples_plots_scatter_11_0.png

Note that how we chose to measure the global importance of a feature will impact the ranking we get. In this example Age is the feature with the largest mean absolute value of the whole dataset, but Capital gain is the feature with the largest absolute impact for any sample.

[11]:
shap.plots.scatter(shap_values[:, shap_values.abs.max(0).argsort[-1]])
../../../_images/example_notebooks_api_examples_plots_scatter_13_0.png

The max function is potentially sensitive to outliers. A more robust option would be to use the percentile function. Here we sort the features by their 95th percentile absolute value and find the Capital gain has the largest 95th percentile value:

[12]:
shap.plots.scatter(shap_values[:, shap_values.abs.percentile(95, 0).argsort[-1]])
../../../_images/example_notebooks_api_examples_plots_scatter_15_0.png

Exploring different interaction colorings

[13]:
# we can use shap.approximate_interactions to guess which features
# may interact with age
inds = shap.utils.potential_interactions(shap_values[:, "Age"], shap_values)

# make plots colored by each of the top three possible interacting features
for i in range(3):
    shap.plots.scatter(shap_values[:,"Age"], color=shap_values[:,inds[i]])
../../../_images/example_notebooks_api_examples_plots_scatter_17_0.png
../../../_images/example_notebooks_api_examples_plots_scatter_17_1.png
../../../_images/example_notebooks_api_examples_plots_scatter_17_2.png

Customizing the figure properties

[3]:
# by passing show=False you can prevent shap.dependence_plot from calling
# the matplotlib show() function, and so you can keep customizing the plot
# before eventually calling show yourself
import matplotlib.pyplot as plt
shap.plots.scatter(shap_values[:,"Age"], show=False)
plt.title("Age dependence plot")
plt.ylabel("SHAP value for the 'Age' feature")
# plt.savefig("my_dependence_plot.pdf") # we can save a PDF of the figure if we want
plt.show()
../../../_images/example_notebooks_api_examples_plots_scatter_19_0.png
[15]:
# you can use xmax and xmin with a percentile notation to hide outliers.
# note that the .percentile method applies to both the .values and .data properties
# of the Explanation object, and the scatter plots knows to use the .data propoerty
# when passed to the xmin or xmax arguments.
age = shap_values[:,"Age"]
shap.plots.scatter(age, xmin=age.percentile(1), xmax=age.percentile(99))
../../../_images/example_notebooks_api_examples_plots_scatter_20_0.png
[16]:
# you can use ymax and ymin with a percentile notation to hide vertical outliers.
# note that now the scatter plot uses the .value property for ymin and ymax if
# an explanation object is passed in those parameters.
age = shap_values[:,"Age"]
shap.plots.scatter(age, ymin=age.percentile(1), ymax=age.percentile(99))
../../../_images/example_notebooks_api_examples_plots_scatter_21_0.png
[17]:
# transparency can help reveal dense vs. sparse areas of the scatter plot
shap.plots.scatter(shap_values[:,"Age"], alpha=0.1)
../../../_images/example_notebooks_api_examples_plots_scatter_22_0.png
[18]:
# transparency can help reveal dense vs. sparse areas of the scatter plot
shap.plots.scatter(shap_values[:,"Age"], dot_size=2, color=shap_values)
../../../_images/example_notebooks_api_examples_plots_scatter_23_0.png
[19]:
# for categorical (or binned) data adding a small amount of x-jitter makes
# thin columns of dots more readable
shap.plots.scatter(shap_values[:,"Age"], dot_size=2, x_jitter=1, color=shap_values)
../../../_images/example_notebooks_api_examples_plots_scatter_24_0.png
[20]:
shap.plots.scatter(shap_values[:,"Age"], dot_size=4, x_jitter=1, color=shap_values, xmin=20, xmax=60, ymin=-1, ymax=2)
../../../_images/example_notebooks_api_examples_plots_scatter_25_0.png
[21]:
# for categorical (or binned) data adding a small amount of x-jitter makes
# thin columns of dots more readable
shap.plots.scatter(shap_values[:,"Relationship"], dot_size=2, x_jitter=0.5, color=shap_values)
../../../_images/example_notebooks_api_examples_plots_scatter_26_0.png
[22]:
import matplotlib.pyplot as plt

# you can use the cmap parameter to provide your own custom color map
shap.plots.scatter(shap_values[:,"Age"], color=shap_values, cmap=plt.get_cmap("cool"))
../../../_images/example_notebooks_api_examples_plots_scatter_27_0.png

Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!