如何计算XGBoost包中的特征分数(/重要性)?

时间:2015-12-11 07:30:37

标签: python r classification feature-selection xgboost

命令xgb.importance会返回以 f得分衡量的功能重要性图表。

f得分代表什么?如何计算?

输出: Graph of feature importance Graph of feature importance

2 个答案:

答案 0 :(得分:20)

这是一个指标,它简单地总结了每个要素拆分的次数。它类似于R版本中的频率指标。https://cran.r-project.org/web/packages/xgboost/xgboost.pdf

它是您可以获得的基本功能重要性指标。

<强>即。这个变量拆分了多少次?

此方法的代码显示它只是在所有树中添加给定特征的存在。

[这里.. https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/core.py#L953][1]

def get_fscore(self, fmap=''):
    """Get feature importance of each feature.
    Parameters
    ----------
    fmap: str (optional)
       The name of feature map file
    """
    trees = self.get_dump(fmap)  ## dump all the trees to text
    fmap = {}                    
    for tree in trees:              ## loop through the trees
        for line in tree.split('\n'):     # text processing
            arr = line.split('[')
            if len(arr) == 1:             # text processing 
                continue
            fid = arr[1].split(']')[0]    # text processing
            fid = fid.split('<')[0]       # split on the greater/less(find variable name)

            if fid not in fmap:  # if the feature id hasn't been seen yet
                fmap[fid] = 1    # add it
            else:
                fmap[fid] += 1   # else increment it
    return fmap                  # return the fmap, which has the counts of each time a  variable was split on

答案 1 :(得分:0)

我发现这个答案是正确和彻底的。它显示了feature_importances的实现。

https://stats.stackexchange.com/questions/162162/relative-variable-importance-for-boosting