python - 如何获取每个特征的 SHAP 值?
问题描述
我目前正在使用 SHAP 库,我已经使用每个功能的平均贡献生成了我的图表,但是我想知道图表上绘制的确切值
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston
import shap
boston = load_boston()
regr = pd.DataFrame(boston.data)
regr.columns = boston.feature_names
regr['MEDV'] = boston.target
X = regr.drop('MEDV', axis = 1)
Y = regr['MEDV']
fit = LinearRegression().fit(X, Y)
explainer = shap.LinearExplainer(fit, X, feature_dependence = 'independent')
# I used 'independent' because the result is consistent with the ordinary
# shapely values where `correlated' is not
shap_values = explainer.shap_values(X)
shap.summary_plot(shap_values, X, plot_type = 'bar')
如何获得图表中描绘的确切值?
解决方案
pd.DataFrame((zip(X.columns[np.argsort(np.abs(shap_values).mean(0))],
np.abs(shap_values).mean(0))), columns=["feature", "importance" ]).sort_values(by="importance", ascending=False)
参考GitHub
推荐阅读
- angular - ng-boostrap 模态 w/o 全屏
- c - 转发流量时无法通过 RAW SOCKET 建立 TCP 连接
- git - 从内部目录使用时 git add 不添加更改
- sql - 创建具有多个字段值的交叉表
- devextreme - 如何在多通道中定位或隐藏幻灯片
- java - 如何从 Protobuf 生成 Scala 文件而不是 Java 文件
- c# - 使用 C# 以编程方式更改 MTU
- swift - 如何覆盖非类成员?
- javascript - 如何完全擦除类的实例
- python - 我正在使用 Django 2.2,我尝试使用 django-star-ratings 0.7.0