【问题标题】:How to get the SHAP values of each feature?如何获取每个特征的 SHAP 值?
【发布时间】:2021-01-21 23:15:15
【问题描述】:

我目前正在使用 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')

我怎样才能得到图表所描绘的确切值?

【问题讨论】:

    标签: python machine-learning shap


    【解决方案1】:
    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

    【讨论】:

      【解决方案2】:

      试试:

      features = X.columns
      mask = np.abs(shap_values).mean(0).argsort()[::-1]
      features[mask]
      

      Index(['LSTAT', 'DIS', 'RAD', 'RM', 'TAX', 'PTRATIO', 'NOX', 'ZN', 'CRIM', 'B',
             'CHAS', 'INDUS', 'AGE'],
            dtype='object')
      

      【讨论】:

        猜你喜欢
        • 2021-04-08
        • 1970-01-01
        • 2022-11-23
        • 2020-12-22
        • 1970-01-01
        • 2015-12-18
        • 2020-09-15
        • 1970-01-01
        • 2020-03-30
        相关资源
        最近更新 更多