赞
踩
shap原理解释
https://yq.aliyun.com/articles/760042?scm=20140722.184.2.173
https://zhuanlan.zhihu.com/p/106320452
shap实用案例
https://zhuanlan.zhihu.com/p/83412330
http://sofasofa.io/tutorials/shap_xgboost/
https://blog.csdn.net/qq_41103204/article/details/104896630
1 首先,准备好机器学习建模,如xgb
train_x, test_x, train_y, test_y = train_test_split(data_input, data_result, test_size=0.01, random_state=0)
params = {......}
model = xgb.train(params, xgb.DMatrix(train_x, label=train_y),......)
2 如果是xgb的1.1.0及以上版本,修改模型解决utf-8不能编码问题
在建模之后,在shap运行之前,添加代码:
# 对model的编码,删除binf
model_modify = model.save_raw()[4:]
def myfun(self=None):
return model_modify
model.save_raw = myfun
3 shap应用
# 调用shap
explainer = shap.TreeExplainer(model)
# 计算shap值
shap_values = explainer.shap_values(train_x)
# 查看结果,以下结果分步单独查看
shap.summary_plot(shap_values, train_x, max_display=30)
# 查看统计条形图,绝对值贡献度
shap.summary_plot(shap_values, train_x, plot_type="bar")
# notebook环境下,加载用于可视化的JS代码
shap.initjs()
# 查看单个样本的值,这里看第0行,即第1个样本的shap图像
# 注:这里的第0行为训练集train_x的第0行,这是从原数据data_input中随机抽出来的第一个数
shap.force_plot(explainer.expected_value, shap_values[0,:], train_x.iloc[0,:])
(1)shap需要传入的内容:
(2)shap_values
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。