【问题标题】:How to get feature importances/feature ranking from summary plot in SHAP without crashing?如何从 SHAP 中的摘要图中获取特征重要性/特征排名而不会崩溃?
【发布时间】:2021-05-09 14:19:01
【问题描述】:

我正在尝试从由

创建的数组中获取 shap 值
explainer = shap.Explainer(xg_clf, X_train)
shap_values2 = explainer(X_train)

使用我的 XGBoost 数据,制作一个包含 feature_names 及其 SHAP 重要性的数据框,因为它们会出现在 SHAP 条或摘要图中。

遵循 how to extract the most important feature names?How to get feature names of shap_values from TreeExplainer? 的建议,特别是用户 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_values 有 11595 个人,每个人有 595 个功能,据我所知,这很大,但是创建 vals 变量运行速度非常慢,在我的笔记本电脑上大约需要 58 分钟。它几乎使用了计算机上的所有 RAM。

58 分钟后出现错误: Command terminated by signal 9

据我了解,这意味着计算机内存不足。

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

feature_importance = pd.DataFrame(list(zip(X_train.columns,np.abs(shap_values2).mean(0))),columns=['col_name','feature_importance_vals'])

这样vals 不会被存储,但此更改根本不会减少 RAM。

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

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

但是 global_shap_importance 以错误的顺序返回特征重要性,我不知道如何更改 global_shap_importance 以便以与 summary_plot 相同的顺序返回特征(蜂群图)。

如何将特征重要性排名放入数据框中?

【问题讨论】:

  • 尝试 1/10 的随机样本。

标签: python-3.x machine-learning shap


【解决方案1】:

我直接从源代码中提取了这个。确认与 summary_plot 相同。

def shapley_feature_ranking(shap_values, X):
    feature_order = np.argsort(np.mean(np.abs(shap_values), axis=0))
    return pd.DataFrame(
        {
            "features": [X.columns[i] for i in feature_order][::-1],
            "importance": [
                np.mean(np.abs(shap_values), axis=0)[i] for i in feature_order
            ][::-1],
        }
    )

【讨论】:

    猜你喜欢
    • 2021-04-08
    • 2020-12-22
    • 2018-06-30
    • 2018-12-06
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2019-08-10
    • 2017-11-14
    相关资源
    最近更新 更多