2

我一直在尝试从shap.summary_plot()RGB 更改为感兴趣的颜色,例如 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 种中间指定一种颜色)?

非常感谢您抽出宝贵的时间,

4

3 回答 3

1

这里所示,我使用图艺术家的 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)

结果

于 2020-03-25T16:33:51.397 回答
0

我不确定您使用的是哪个版本的 SHAP,但在 0.4.0 (02-2022) 版本中,摘要图具有 cmap 参数,因此您可以直接将构建的 cmap 传递给它:

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

于 2022-02-12T20:35:13.320 回答
0

实际上我把它作为这个 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)
于 2021-07-29T13:21:57.587 回答