Xgboost中特征重要性计算方法详解

1.plot_importance方法

xgboost中的plot_importance方法内置了几种计算重要性的方式。

def plot_importance(booster, ax=None, height=0.2,
                    xlim=None, ylim=None, title='Feature importance',
                    xlabel='F score', ylabel='Features',
                    importance_type='weight', max_num_features=None,
                    grid=True, show_values=True, **kwargs):
    """Plot importance based on fitted trees.

    Parameters
    ----------
    booster : Booster, XGBModel or dict
        Booster or XGBModel instance, or dict taken by Booster.get_fscore()
    ax : matplotlib Axes, default None
        Target axes instance. If None, new figure and axes will be created.
    grid : bool, Turn the axes grids on or off.  Default is True (On).
    importance_type : str, default "weight"
        How the importance is calculated: either "weight", "gain", or "cover"

        * "weight" is the number of times a feature appears in a tree
        * "gain" is the average gain of splits which use the feature
        * "cover" is the average coverage of splits which use the feature
          where coverage is defined as the number of samples affected by the split
    max_num_features : int, default None
        Maximum number of top features displayed on plot. If None, all features will be displayed.
    height : float, default 0.2
        Bar height, passed to ax.barh()
    xlim : tuple, default None
        Tuple passed to axes.xlim()
    ylim : tuple, default None
        Tuple passed to axes.ylim()
    title : str, default "Feature importance"
        Axes title. To disable, pass None.
    xlabel : str, default "F score"
        X axis title label. To disable, pass None.
    ylabel : str, default "Features"
        Y axis title label. To disable, pass None.
    show_values : bool, default True
        Show values on plot. To disable, pass False.
    kwargs :
        Other keywords passed to ax.barh()

    Returns
    -------
    ax : matplotlib Axes
    """

plot_importance的方法签名如上所示。

从上面的方法签名可以看出
1.如果没有指定坐标轴名称,默认的x轴名称为"F score",y轴名称为"Features"。
2.重要性计算类型有三种,分别为weight, gain, cover,下面我们针对这三种计算类型进行总结。

2.weight

* "weight" is the number of times a feature appears in a tree

从上面的解释不难看出,weight方法衡量特征重要性的计算方式,是在子树进行分裂的时候,用到的特征次数,而且这里指的是所有的树。

一般来说,weight会给数值特征更高的值。因为连续值的变化越多,树分裂时候可以切割的空间就越大,那被用到的次数也就越多。所以对于weight指标,比较容易掩盖重要的枚举类特征。

3.gain

* "gain" is the average gain of splits which use the feature

gain采用的计算熵的方式。如果按某个特征进行分裂,熵的增量比较大,那么该特征的重要性就越强。
与特征选择里面采用计算信息增益的方式是一样的。

4.cover

* "cover" is the average coverage of splits which use the feature
          where coverage is defined as the number of samples affected by the split

cover的计算方法是,树在进行分列时,特征下面的叶子结点涵盖的样本数除以特征用来分裂的次数。当分裂越靠近树的根部时,cover的值会越大。

cover 对于枚举特征会更合适。同时,它也没有过度拟合目标函数,不会受目标函数的量纲影响。

5.permutation_importance方法

除此以外,还有permutation_importance方法也可以做衡量特征重要性的工作。sklearn官方文档针对该方法的说明如下

Permutation feature importance is a model inspection technique that can be used for any fitted estimator when the data is tabular.
This is especially useful for non-linear or opaque estimators.
The permutation feature importance is defined to be the decrease in a model score when a single feature value is randomly shuffled.
This procedure breaks the relationship between the feature and the target, 
thus the drop in the model score is indicative of how much the model depends on the feature. 
This technique benefits from being model agnostic and can be calculated many times with different permutations of the feature.

其原理大致如下:
1.首先根据训练集训练一个模型。
2.在测试集上测试该模型,得到模型相关的指标,比如回归问题为MSE,分类问题为logloss或者auc之类的指标。
3.在测试集上将某一个特征进行randomly shuffle(随机替换该特征值),在使用模型进行预测,得到新的模型指标。与第2步得到的指标进行比较,如果相差越多,说明特征的重要性越大。

  • 8
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随机森林(Random Forest)是一种集成学习方法,它由多个决策树组成。在构建随机森林时,我们需要从数据集有放回地抽取样本,然后基于随机选择的特征来构建多棵决策树。最终,随机森林的预测结果是多棵决策树的平均值或投票结果。 在随机森林特征重要性评估是一项重要的任务。特征重要性评估可以帮助我们了解哪些特征在预测起到了重要的作用,从而帮助我们进行特征选择和模型优化。随机森林特征重要性评估方法主要有两种: 1. 基于 Gini 指数的特征重要性评估 Gini 指数是一种衡量决策树分类效果的指标,它可以用来评估随机森林每个特征重要性。具体来说,对于每个特征,我们可以计算出所有决策树上使用该特征的节点的 Gini 指数之和,并将其作为该特征重要性评分。这种方法认为,在所有决策树使用某个特征的节点所得到的分类效果越好,该特征重要性就越高。 2. 基于特征重要性的排列方法 这种方法的原理比较简单,它通过随机打乱某个特征的值,来衡量该特征对模型的影响力。具体来说,我们可以对某个特征的所有样本进行随机重排,然后重新计算模型的预测结果。如果重新排列后的预测结果变化很大,说明该特征对模型的影响力很大;反之,如果变化很小,说明该特征对模型的影响力不大。这种方法可以避免基于 Gini 指数的方法可能存在的一些问题,比如无法识别特征之间的相互作用等。 总的来说,随机森林特征重要性评估方法可以帮助我们选择最重要的特征,从而提高模型的预测性能。不同的评估方法有不同的优缺点,我们可以根据具体情况选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值