The dependence and summary plots create Python matplotlib plots that can be customized at will. However, the force plots generate plots in Javascript, which are harder to modify inside a notebook. In the case that the colors of the force plot want to be modified, the plot_cmap parameter can be used to change the force plot colors.
In [1]:
import xgboost
import shap
# load JS visualization code to notebook
shap.initjs()
# train XGBoost model
X,y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
# explain the model's predictions using SHAP values
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)
In [2]:
# visualize the first prediction's explaination with default colors
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])
Out[2]:
The plot color maps are named based on the color maps on matplotlib.
In [3]:
# visualize the first prediction's explaination with a Pink and Yellow Green color map.
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:], plot_cmap="PkYg")
Out[3]:
The same can be applied to lists of explanations
In [4]:
# visualize the first 5 predictions explanations with a dark red dark blue color map.
shap.force_plot(explainer.expected_value, shap_values[0:5,:], X.iloc[0:5,:], plot_cmap="DrDb")
Out[4]:
The different color map names are available in the color-set.js file. To add one, modify the file with a color map name, and a list containing the two colors of the color map, the first one being the one for positive SHAP values, and the second one for the negative SHAP values. You can also pass a list of colors as the value for the plot_cmap parameter to render the force plot with those colors. Those colors must be hex-coded.
In [5]:
# visualize the first 5 predictions explanations with a custom color map.
shap.force_plot(explainer.expected_value, shap_values[0:5,:], X.iloc[0:5,:], plot_cmap=["#FF5733","#335BFF"])
Out[5]: