python解释模型库Shap实现机器学习模型输出可视化
作者:Python学习与数据挖掘 时间:2022-04-27 08:00:10
解释一个机器学习模型是一个困难的任务,因为我们不知道这个模型在那个黑匣子里是如何工作的。解释是必需的,这样我们可以选择最佳的模型,同时也使其健壮。
我们开始吧…
安装所需的库
使用pip安装Shap开始。下面给出的命令可以做到这一点。
pip install shap
导入所需库
在这一步中,我们将导入加载数据、创建模型和创建该模型的可视化所需的库。
df = pd.read_csv('/content/Diabetes.csv')
features = ['Pregnancies', 'Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
Y = df['Outcome']
X = df[features]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 1234)
xgb_model = xgb.XGBRegressor(random_state=42)
xgb_model.fit(X_train, Y_train)
创建模型
在这一步中,我们将创建机器学习模型。在本文中,我将创建一个XGBoost模型,但是你可以选择任何模型。我们将用于此模型的数据集是著名的糖尿病数据集,可从Kaggle下载。
df = pd.read_csv('/content/Diabetes.csv')
features = ['Pregnancies', 'Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
Y = df['Outcome']
X = df[features]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 1234)
xgb_model = xgb.XGBRegressor(random_state=42)
xgb_model.fit(X_train, Y_train)
创建可视化
现在我们将为shap创建解释程序,找出模型的shape值,并使用它们创建可视化效果。
explainer = shap.Explainer(xgb_model)
shap_values = explainer(X_test)
1、Bar Plot
shap.plots.bar(shap_values, max_display=10)
2、队列图
shap.plots.bar(shap_values.cohorts(2).abs.mean(0))
3、热图
shap.plots.heatmap(shap_values[1:100])
4、瀑布图
shap.plots.waterfall(shap_values[0]) # For the first observation
5、力图
shap.initjs()
explainer = shap.TreeExplainer(xgb_model)
shap_values = explainer.shap_values(X_test)
def p(j):
return(shap.force_plot(explainer.expected_value, shap_values[j,:], X_test.iloc[j,:]))
p(0)
6、决策图
shap_values = explainer.shap_values(X_test)[1]
print("The expected value is ", expected_value)
print("The final prediction is ", xgb_model.predict(X_test)[1])
shap.decision_plot(expected_value, shap_values, X_test)
这就是如何使用 Shap 创建与机器学习模型相关的可视化并对其进行分析。
来源:https://blog.csdn.net/weixin_38037405/article/details/118341218
标签:Shap,解释模型,可视化,机器学习
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python开发工具Pycharm的安装以及使用步骤总结
2022-09-15 08:21:01
![](https://img.aspxhome.com/file/2023/3/72893_0s.png)
用ASP实现IE地址栏参数的判断
2008-10-10 15:54:00
MacOS中 VSCode 安装 GO 插件失败问题的快速解决方法
2023-06-18 02:32:32
10个不为人知的Google失败作品
2008-05-24 16:56:00
![](https://img.aspxhome.com/file/UploadPic/20085/24/200852417924655s.jpg)
在ORACLE移动数据库文件
2010-08-02 12:54:00
Python实战使用XPath采集数据示例解析
2022-02-25 09:00:24
![](https://img.aspxhome.com/file/2023/4/72024_0s.png)
富文本编辑器的基本原理与实践
2008-06-13 13:28:00
Python实现读取Properties配置文件的方法
2021-01-05 17:28:21
Python基于Django实现验证码登录功能
2023-06-25 03:21:13
![](https://img.aspxhome.com/file/2023/8/72238_0s.png)
Python 树表查找(二叉排序树、平衡二叉树)
2021-01-27 03:06:20
![](https://img.aspxhome.com/file/2023/8/64318_0s.png)
Python 列表理解及使用方法
2023-08-29 06:55:02
oracle 11g的警告日志和监听日志的删除方法
2023-06-29 13:59:36
python实现会员管理系统
2023-11-13 19:44:46
![](https://img.aspxhome.com/file/2023/2/65872_0s.jpg)
Oracle捕获问题SQL解决CPU过渡消耗
2010-07-21 13:14:00
SQL Server连接中经常出现的3个常见错误与解答
2010-07-26 14:25:00
javascript基础之数组(Array)对象
2008-06-25 13:32:00
Python列表去重复项的N种方法(实例代码)
2023-06-27 16:00:20
![](https://img.aspxhome.com/file/2023/0/60710_0s.jpg)
泛泛而谈界面视觉效果的一致性
2010-01-05 17:05:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/0912-ui-conformity-5-22s.jpg)
asp 输出换行的详细说明 原创
2011-02-24 11:15:00
MySQL Enterprise备份的恢复解决方案
2011-12-14 18:36:25