如何计算XGBoost包中的功能评分(/重要性)?

2024-05-11 18:55:23 发布

您现在位置:Python中文网/ 问答频道 /正文

命令xgb.importance返回由f score度量的特征重要性图。

这个分数代表什么?它是如何计算的?

输出: Graph of feature importanceGraph of feature importance


Tags: of命令度量代表特征分数重要性feature
2条回答

这是一个度量标准,它简单地总结了每个功能被拆分的次数。它类似于R版本中的频率度量

这是一个基本的特征重要性度量,你可以得到。

也就是说,这个变量被拆分了多少次?

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

[这里..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

我发现这个答案是正确和彻底的。它展示了特性的实现。

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

相关问题 更多 >