将 `shap.summary_plot()` 的渐变颜色更改为特定的 2 或 3 RGB 渐变调色板颜色

Changing the gradient color of `shap.summary_plot()` to specific 2 or 3 RGB gradient palette Colors

我一直在尝试将渐变调色板颜色从 shap.summary_plot() 更改为感兴趣的颜色,例如 RGB。

为了说明这一点,我尝试使用 matplotlib 来创建我的调色板。但是,到目前为止它还没有奏效。 有人可以帮我吗?

这是我目前尝试过的: 使用 iris 数据集创建示例(这里没有问题)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb
import shap

# import some data to play with
iris = datasets.load_iris()
Y = pd.DataFrame(iris.target, columns = ["Species"])
X = pd.DataFrame(iris.data, columns = iris.feature_names)


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=0, stratify=Y)

params = { # General Parameters
            'booster': 'gbtree',
            # Param for boosting
             'eta': 0.2, 
             'gamma': 1,
             'max_depth': 5,
             'min_child_weight': 5,
             'subsample': 0.5,
             'colsample_bynode': 0.5,             
             'lambda': 0,  #default = 0                                        
             'alpha': 1,    #default = 1            
            # Command line parameters
             'num_rounds': 10000,
            # Learning Task Parameters
             'objective': 'multi:softprob' #'multi:softprob'
             }


model = xgb.XGBClassifier(**params, verbose=0, cv=5 , )
# fitting the model
model.fit(X_train,np.ravel(Y_train), eval_set=[(X_test, np.ravel(Y_test))], early_stopping_rounds=20)
# Tree on XGBoost
explainerXGB = shap.TreeExplainer(model, data=X, model_output ="margin")
#recall one  can put "probablity"  then we explain the output of the model transformed 
#into probability space (note that this means the SHAP values now sum to the probability output of the model).
shap_values_XGB_test = explainerXGB.shap_values(X_test)
shap_values_XGB_train = explainerXGB.shap_values(X_train)

shap.summary_plot(shap_values_XGB_train, X_train, )#color=cmap

到这里为止,如果您 运行 应该使用默认颜色获取摘要图的代码。为了更改默认值,我尝试创建我的 2 颜色渐变调色板,如下所示:

from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap

RGB_val = 255

color01= (0,150,200)  # Blue wanted
color04= (220,60,60)  # red wanted
Colors = [color01, color04]

# Creating a blue red palette transition for graphics
Colors= [(R/RGB_val,G/RGB_val,B/RGB_val) for idx, (R,G,B) in enumerate(Colors)]
n = 256

# Start of the creation of the gradient
Color01= ListedColormap(Colors[0], name='Color01', N=None)
Color04= ListedColormap(Colors[1], name='Color04', N=None)
top = cm.get_cmap(Color01,128)
bottom = cm.get_cmap(Color04,128)
newcolors = np.vstack((top(np.linspace(0, 1, 128)),
                       bottom(np.linspace(0, 1, 128))))

mymin0 = newcolors[0][0]
mymin1 = newcolors[0][1]
mymin2 = newcolors[0][2]
mymin3 = newcolors[0][3]
mymax0 = newcolors[255][0]
mymax1 = newcolors[255][1]
mymax2 = newcolors[255][2]
mymax3 = newcolors[255][3]

GradientBlueRed= [np.linspace(mymin0, mymax0,  n),
                   np.linspace(mymin1, mymax1,  n),
                   np.linspace(mymin2, mymax2,  n),
                   np.linspace(mymin3, mymax3,  n)]

GradientBlueRed_res =np.transpose(GradientBlueRed)

# End of the creation of the gradient

newcmp = ListedColormap(GradientBlueRed_res, name='BlueRed')

shap.summary_plot(shap_values_XGB_train, X_train, color=newcmp)

但是我无法更改图形的颜色。 :

谁能告诉我如何制作:

(A) 2渐变色或 (B) 3个颜色渐变(指定一个颜色在其他2个中间) ?

非常感谢您提前抽出时间,

如前所述here,我的解决方法是使用人物艺术家的 set_cmap() 函数:

# Create colormap
newcmp = ListedColormap(GradientBlueRed_res, name='BlueRed')

# Plot the summary without showing it
plt.figure()
shap.summary_plot(shap_values_XGB_train, X_train, show=False)

# Change the colormap of the artists
for fc in plt.gcf().get_children():
    for fcc in fc.get_children():
        if hasattr(fcc, "set_cmap"):
            fcc.set_cmap(newcmp)

Result

实际上我把它作为这个 SHAP 图的解决方案(当前版本是 0.39)。基本上你可以生成一个cmap然后通过参数cmap.

来使用它

一个例子:

import shap
from matplotlib.colors import LinearSegmentedColormap

# Generate colormap through matplotlib
newCmap = LinearSegmentedColormap.from_list("", ['#c4cfd4','#3345ea'])

# Set plot
shap.decision_plot(..., plot_color=newCmap)

我不确定你用的是哪个版本的SHAP,但是在0.4.0版本(02-2022)summary plot中有cmap参数,所以你可以直接将你构建的cmap传递给它:

    shap.summary_plot(shap_values, plot_type='dot', plot_size=(12, 6), cmap='hsv')