【问题标题】:How to get CORRECT feature importance plot in XGBOOST?如何在 XGBOOST 中获得正确的特征重要性图?
【发布时间】:2020-03-17 22:47:05
【问题描述】:

在 XGBOOST 特征重要性中使用两种不同的方法,给了我两个不同的最重要的特征,应该相信哪一个?

什么时候应该使用什么方法?我很困惑。

设置

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline

import seaborn as sns
import xgboost as xgb

df = sns.load_dataset('mpg')
df = df.drop(['name','origin'],axis=1)

X = df.iloc[:,1:]
y = df.iloc[:,0]

Numpy 数组

# fit the model
model_xgb_numpy = xgb.XGBRegressor(n_jobs=-1,objective='reg:squarederror')
model_xgb_numpy.fit(X.to_numpy(), y.to_numpy())

plt.bar(range(len(model_xgb_numpy.feature_importances_)), model_xgb_numpy.feature_importances_)

熊猫数据框

# fit the model
model_xgb_pandas = xgb.XGBRegressor(n_jobs=-1,objective='reg:squarederror')
model_xgb_pandas.fit(X, y)
axsub = xgb.plot_importance(model_xgb_pandas)

问题

Numpy 方法显示第 0 个特征圆柱是最重要的。 Pandas 方法表明车型年份是最重要的。哪个是正确的最重要的功能?

参考文献

【问题讨论】:

    标签: python xgboost


    【解决方案1】:

    很难定义正确的特征重要性度量。每个都有优点和缺点。这是一个广泛的话题,目前还没有黄金法则,我个人建议阅读 Christoph Molnar 的这本在线书籍:https://christophm.github.io/interpretable-ml-book/。这本书对不同的度量和不同的算法进行了很好的概述。

    根据经验,如果您不能使用外部包,我会选择gain,因为它更能代表人们感兴趣的内容(通常对特定的拆分的原始发生不感兴趣)功能,而是这些拆分有多大帮助),请参阅此问题以获得很好的总结:https://datascience.stackexchange.com/q/12318/53060。如果您可以使用其他工具,shap 表现出非常好的行为,我会始终选择它而不是内置 xgb 树度量,除非计算时间受到强烈限制。

    至于您在问题中直接指出的差异,差异的根源在于xgb.plot_importance使用weight作为默认提取的特征重要性类型,而XGBModel本身使用@987654328 @ 作为默认类型。如果您将它们配置为使用相同的重要性类型,那么您将获得相似的分布(在feature_importance_ 中进行额外的归一化并在plot_importance 中进行排序)。

    【讨论】:

      【解决方案2】:

      有 3 种方法可以从 Xgboost 中获取特征重要性:

      • 使用内置的特征重要性(我更喜欢gain类型),
      • 使用基于排列的特征重要性
      • 使用 SHAP 值计算特征重要性

      在我的post 中,我为所有 3 种方法编写了代码示例。就个人而言,我使用的是基于排列的特征重要性。在我看来,内置的特征重要性可以在过度拟合数据后将特征显示为重要(这只是基于我的经验的观点)。 SHAP 解释很棒,但有时计算它们可能很耗时(并且您需要对数据进行下采样)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-07
        • 2019-04-15
        • 2019-07-22
        • 2019-12-13
        • 2020-08-20
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多