xgboost是基于GBDT原理进行改进的算法,效率高,并且可以进行并行化运算;

而且可以在训练的过程中给出各个特征的评分,从而表明每个特征对模型训练的重要性,

调用的源码就不准备详述,本文主要侧重的是计算的原理,函数get_fscore源码如下,

源码来自安装包:xgboost/python-package/xgboost/core.py

通过下面的源码可以看出,特征评分可以看成是被用来分离决策树的次数,而这个与

《统计学习基础-数据挖掘、推理与推测》中10.13.1 计算公式有写差异,此处需要注意。

注:考虑的角度不同,计算方法略有差异。

  1. def get_fscore(self, fmap=''):
  2. """Get feature importance of each feature.
  3.  
  4. Parameters
  5. ----------
  6. fmap: str (optional)
  7. The name of feature map file
  8. """
  9.  
  10. return self.get_score(fmap, importance_type='weight')
  11.  
  12. def get_score(self, fmap='', importance_type='weight'):
  13. """Get feature importance of each feature.
  14. Importance type can be defined as:
  15. 'weight' - the number of times a feature is used to split the data across all trees.
  16. 'gain' - the average gain of the feature when it is used in trees
  17. 'cover' - the average coverage of the feature when it is used in trees
  18.  
  19. Parameters
  20. ----------
  21. fmap: str (optional)
  22. The name of feature map file
  23. """
  24.  
  25. if importance_type not in ['weight', 'gain', 'cover']:
  26. msg = "importance_type mismatch, got '{}', expected 'weight', 'gain', or 'cover'"
  27. raise ValueError(msg.format(importance_type))
  28.  
  29. # if it's weight, then omap stores the number of missing values
  30. if importance_type == 'weight':
  31. # do a simpler tree dump to save time
  32. trees = self.get_dump(fmap, with_stats=False)
  33.  
  34. fmap = {}
  35. for tree in trees:
  36. for line in tree.split('\n'):
  37. # look for the opening square bracket
  38. arr = line.split('[')
  39. # if no opening bracket (leaf node), ignore this line
  40. if len(arr) == 1:
  41. continue
  42.  
  43. # extract feature name from string between []
  44. fid = arr[1].split(']')[0].split('<')[0]
  45.  
  46. if fid not in fmap:
  47. # if the feature hasn't been seen yet
  48. fmap[fid] = 1
  49. else:
  50. fmap[fid] += 1
  51.  
  52. return fmap
  53.  
  54. else:
  55. trees = self.get_dump(fmap, with_stats=True)
  56.  
  57. importance_type += '='
  58. fmap = {}
  59. gmap = {}
  60. for tree in trees:
  61. for line in tree.split('\n'):
  62. # look for the opening square bracket
  63. arr = line.split('[')
  64. # if no opening bracket (leaf node), ignore this line
  65. if len(arr) == 1:
  66. continue
  67.  
  68. # look for the closing bracket, extract only info within that bracket
  69. fid = arr[1].split(']')
  70.  
  71. # extract gain or cover from string after closing bracket
  72. g = float(fid[1].split(importance_type)[1].split(',')[0])
  73.  
  74. # extract feature name from string before closing bracket
  75. fid = fid[0].split('<')[0]
  76.  
  77. if fid not in fmap:
  78. # if the feature hasn't been seen yet
  79. fmap[fid] = 1
  80. gmap[fid] = g
  81. else:
  82. fmap[fid] += 1
  83. gmap[fid] += g
  84.  
  85. # calculate average value (gain/cover) for each feature
  86. for fid in gmap:
  87. gmap[fid] = gmap[fid] / fmap[fid]
  88.  
  89. return gmap

GBDT特征评分的计算说明原理:

链接:1、http://machinelearningmastery.com/feature-importance-and-feature-selection-with-xgboost-in-python/

详细的代码说明过程:可以从上面的链接进入下面的链接:

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

【原创】xgboost 特征评分的计算原理的更多相关文章

  1. XGBboost 特征评分的计算原理

    xgboost是基于GBDT原理进行改进的算法,效率高,并且可以进行并行化运算,而且可以在训练的过程中给出各个特征的评分,从而表明每个特征对模型训练的重要性, 调用的源码就不准备详述,本文主要侧重的是 ...

  2. (原创)sklearn中 F1-micro 与 F1-macro区别和计算原理

    最近在使用sklearn做分类时候,用到metrics中的评价函数,其中有一个非常重要的评价函数是F1值,(关于这个值的原理自行google或者百度) 在sklearn中的计算F1的函数为 f1_sc ...

  3. xgboost入门与实战(原理篇)

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  4. Security:蠕虫的行为特征描述和工作原理分析

    ________________________ 参考: 百度文库---蠕虫的行为特征描述和工作原理分析 http://wenku.baidu.com/link?url=ygP1SaVE4t4-5fi ...

  5. OpenGL中摄像机矩阵的计算原理

    熟悉OpenGL|ES的朋友,可能会经常设置摄像机的view矩阵,iOS中相对较好,已经封装了方向,只需要设置摄像机位置,目标点位置以及UP向量即可.下面先介绍下摄像机view矩阵的计算原理.此处假设 ...

  6. 005-hive概述,计算原理及模型

    计算原理及模型 优化的根本思想: 尽早尽量过滤数据,减少每个阶段的数据量 减少job数 解决数据倾斜问题 Hive概述 名称       hive系统架构 metastore derbymysql   ...

  7. 基于IG的特征评分方法

    本文简单介绍了熵.信息增益的概念,以及如何使用信息增益对监督学习的训练样本进行评估,评估每个字段的信息量. 1.熵的介绍       在信息论里面,熵是对不确定性的测量.通俗来讲,熵就是衡量随机变量随 ...

  8. 全基因组关联分析(GWAS)的计算原理

    前言 关于全基因组关联分析(GWAS)原理的资料,网上有很多. 这也是我写了这么多GWAS的软件教程,却从来没有写过GWAS计算原理的原因. 恰巧之前微博上某位小可爱提问能否写一下GWAS的计算原理. ...

  9. 前端移动端的rem适配计算原理

    rem是什么? rem(font size of the root element)是指相对于根元素的字体大小的单位.简单的说它就是一个相对单位.看到rem大家一定会想起em单位,em(font si ...

随机推荐

  1. 读书笔记 --TCP :传输控制协议(二)

    TCP建立连接 请求端(客户端)发送一个SYN指明客户端打算连接的服务器端口号,以及初始序列号. 服务端发回包含服务器的初始序号的SYN报文段作为应答.同时,将确认序号设置为客户的ISN加1以对客户的 ...

  2. python 编辑xml中namespace问题中出现很多ns0

    原始文件:

  3. svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录

    svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录 Windows下,svn服务器IP本来是内网一台服务器上 ...

  4. Java三行代码搞定MD5加密,测试5c短信网关的demo

    看到之前项目中,关于MD5加密的足足写了一个辅助类. 其实在Java中大部分都帮你实现好了,完成MD5加密,主要就三行代码: /** * 对字符串md5加密 * * @param str * @ret ...

  5. jquery阻止冒泡事件行为发生

    <div onclick="a()"> <p onclick="b()"></p> </div> div和p元素 ...

  6. <基督教福音视频>

    <信耶稣还是罪人吗?>全集 http://www.youku.com/playlist_show/id_18458615.html <因信称义>全集 http://www.yo ...

  7. python:Django

    Python的WEB框架有Django.Tornado.Flask 等多种 web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客 ...

  8. Java8闭包

    闭包在很多语言中都存在,例如C++,C#.闭包允许我们创建函数指针,并把它们作为参数传递,Java编程语言提供了接口的概念,接口中可以定义抽象方法,接口定义了API,并希望用户或者供应商来实现这些方法 ...

  9. Jquery 下实现 图片大图预览效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. 测试机安装fd-server问题记录

    今天在239测试机上安装了fd-server来代替apache,汇总下遇到的问题和解决方法. 1. 安装git时使用yum安装,命令 yum install git 2. 启动fd-server之前要 ...