Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在不崩溃的情况下从SHAP中的摘要图中获取特征重要性/特征排名?_Python 3.x_Machine Learning_Shap - Fatal编程技术网

Python 3.x 如何在不崩溃的情况下从SHAP中的摘要图中获取特征重要性/特征排名?

Python 3.x 如何在不崩溃的情况下从SHAP中的摘要图中获取特征重要性/特征排名?,python-3.x,machine-learning,shap,Python 3.x,Machine Learning,Shap,我试图从由创建的数组中获取shap值 explainer = shap.Explainer(xg_clf, X_train) shap_values2 = explainer(X_train) 使用我的XGBoost数据,创建要素名称及其形状重要性的数据框,就像它们出现在形状栏或摘要图中一样 下面是来自用户Thoo的建议,特别是用户Thoo的注释,其中显示了如何提取值以生成数据帧: vals= np.abs(shap_values).mean(0) feature_importance = p

我试图从由创建的数组中获取shap值

explainer = shap.Explainer(xg_clf, X_train)
shap_values2 = explainer(X_train)
使用我的XGBoost数据,创建要素名称及其形状重要性的数据框,就像它们出现在形状栏或摘要图中一样

下面是来自用户Thoo的建议,特别是用户Thoo的注释,其中显示了如何提取值以生成数据帧:

vals= np.abs(shap_values).mean(0)
feature_importance = pd.DataFrame(list(zip(X_train.columns,vals)),columns=['col_name','feature_importance_vals'])
feature_importance.sort_values(by=['feature_importance_vals'],ascending=False,inplace=True)
feature_importance.head()
shap_值有11595人,每个人有595个功能,我知道这是一个很大的值,但是创建
vals
变量运行非常缓慢,在我的笔记本电脑上大约需要58分钟。它使用计算机上几乎所有的RAM

58分钟后,我发现一个错误:
由信号9终止的命令

据我所知,这意味着电脑内存不足

我试着将Thoo代码中的第二行转换为

feature_importance = pd.DataFrame(list(zip(X_train.columns,np.abs(shap_values2).mean(0))),columns=['col_name','feature_importance_vals'])
这样就不会存储
VAL
,但这一更改根本不会减少RAM

我还尝试了同一GitHub问题的不同评论(用户“ba1mn”):

但是
global\u-shap\u-importance
以错误的顺序返回要素重要性,我不知道如何更改
global\u-shap\u-importance
,使要素以与
summary\u-plot
相同的顺序返回


如何将功能重要性排序放入数据框?

尝试1/10的随机样本。
def global_shap_importance(model, X):
    """ Return a dataframe containing the features sorted by Shap importance
    Parameters
    ----------
    model : The tree-based model 
    X : pd.Dataframe
         training set/test set/the whole dataset ... (without the label)
    Returns
    -------
    pd.Dataframe
        A dataframe containing the features sorted by Shap importance
    """
    explainer = shap.Explainer(model)
    shap_values = explainer(X)
    cohorts = {"": shap_values}
    cohort_labels = list(cohorts.keys())
    cohort_exps = list(cohorts.values())
    for i in range(len(cohort_exps)):
        if len(cohort_exps[i].shape) == 2:
            cohort_exps[i] = cohort_exps[i].abs.mean(0)
    features = cohort_exps[0].data
    feature_names = cohort_exps[0].feature_names
    values = np.array([cohort_exps[i].values for i in range(len(cohort_exps))])
    feature_importance = pd.DataFrame(
        list(zip(feature_names, sum(values))), columns=['features', 'importance'])
    feature_importance.sort_values(
        by=['importance'], ascending=False, inplace=True)
    return feature_importance