1. shap version check and building a explainer
2. matplotlib colormap, make color dictionary
3. shap.summary_plot
1. shap version check and building a explainer
사용한 shap version : 0.37.0
import shap
shap.initjs()
print(shap.__version__)
building a explainer
# changing names of features
names = pd.read_csv('~/name_dictionary.csv', index_col='var')
background = shap.maskers.Independent(X_train_features_new, max_samples=100)
# type of model is XGBoost, output of explainer's output is probability
explainer = shap.TreeExplainer(ML_model, model_output='probability', data=X_train_features_new)
# calculate the shap values
shap_values = explainer.shap_values(X_train_features_new)
# set the dictionary of feature's name
meaning_dict = names.to_dict()['name']
보통 shap summary plot을 그리면 아래 그림과 같음
# SHAP, train set
shap.summary_plot(shap_values, X_train_features_new.rename(columns=meaning_dict))
2. matplotlib colormap, make color dictionary
기본 스타일도 좋지만 흑백으로 하고 싶을 때 matplotlib.colors를 이용해서 맵핑 작업을 해주면 됨.
#shap gray version configuration
import matplotlib.colors as colors
gray_val=0.75
cdict = {'red': [(0.0, gray_val, gray_val),
(1.0, 0.0, 0.0)],
'green': [(0.0, gray_val, gray_val),
(1.0, 0.0, 0.0)],
'blue': [(0.0, gray_val, gray_val),
(1.0, 0.0, 0.0)]}
gray2black = colors.LinearSegmentedColormap(name='red', segmentdata=cdict)
3. shap.summary_plot
# SHAP,test all / gray-scale version
shap_values_all = explainer.shap_values(X_test_features)
shap.summary_plot(shap_values_all, X_test_features.rename(columns=meaning_dict), show=False,
cmap=plt.get_cmap(gray2black))
plt.tight_layout()
plt.show()