将xgboost的plot_importance绘图时出现的f0、f1、f2、f3、f4、f5等改为对应特征的字段名

xgboost输出特征重要度

操作总结

进入xgboost.plot_importance函数定义, plotting.py , 把 booster.get_score(importance_type=importance_type) 改成 booster.get_score(importance_type=importance_type, fmap=fmap) 亲测好使

代码部分

# 调用文件
import xgboost as xgb
import numpy as np
import json
import matplotlib.pyplot as plt
from matplotlib import pyplot
from xgboost import plot_treemodel_file = "data/xgboost.model"
fmap = model_file + ".fmap"
bst = xgb.Booster(model_file = model_file)
print bst.get_fscore(fmap=fmap)
xgb.to_graphviz(bst, num_trees=10, fmap=fmap)
xgb.plot_importance(bst, fmap=fmap)

只需修改第67行

# plot_importance修改
# Python/2.7/lib/python/site-packages/xgboost/plotting.py
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, fmap='', **kwargs):"""Plot importance based on fitted trees.Parameters----------booster : Booster, XGBModel or dictBooster or XGBModel instance, or dict taken by Booster.get_fscore()ax : matplotlib Axes, default NoneTarget 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 featurewhere coverage is defined as the number of samples affected by the splitmax_num_features : int, default NoneMaximum number of top features displayed on plot. If None, all features will be displayed.height : float, default 0.2Bar height, passed to ax.barh()xlim : tuple, default NoneTuple passed to axes.xlim()ylim : tuple, default NoneTuple 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 TrueShow values on plot. To disable, pass False.kwargs :Other keywords passed to ax.barh()Returns-------ax : matplotlib Axes"""# TODO: move this to compat.pytry:import matplotlib.pyplot as pltexcept ImportError:raise ImportError('You must install matplotlib to plot importance')if isinstance(booster, XGBModel):importance = booster.get_booster().get_score(importance_type=importance_type)elif isinstance(booster, Booster):# 只是在这里添加了fmap 其余都一样 只改这个函数这一行importance = booster.get_score(importance_type=importance_type, fmap=fmap)elif isinstance(booster, dict):importance = boosterelse:raise ValueError('tree must be Booster, XGBModel or dict instance')

fmap生成

features2 = ['core_id', 'what_id', 'where_id', 'extra_id', 'category_group_id', 'chain_id', 'address_id']if __name__=="__main__":f1 = open(sys.argv[1], "w") for i, feat in enumerate(features2):      f1.write('{0}\t{1}\tq\n'.format(i, feat))        #feature type, use i for indicator and q for quantity  outfile.close()print("Done!")

xgboost的plot_importance绘图时出现的f0、f1、f2、f3、f4、f5等改为对应特征的字段名相关推荐

  1. 成功解决 将xgboost的plot_importance绘图时出现的f0、f1、f2、f3、f4、f5等改为对应特征的字段名

    成功解决 将xgboost的plot_importance绘图时出现的f0.f1.f2.f3.f4.f5等改为对应特征的字段名 目录 解决后的结果 解决问题 解决思路 解决方法 解决后的结果

  2. 树莓派 st-link master使用ST-LINK V2下载STM32程序 支持F0 F1 F2 等

    这段时间在研究怎么脱机实现STM32的下载,同时也了解了Windows和Linux的下载方式,st官方只提供了Windows的下载工具,Linux的版本并未提供,这里网上搜索后找到了st-link m ...

  3. 成功解决 绘图时行坐标名或列坐标名出现f0、f1、f2、f3、f4、f5(或者Column_0、Column_1、Column_2、Column_3)等命名而不是想要的具体对应字段命名

    成功解决 绘图时行坐标名或列坐标名出现f0.f1.f2.f3.f4.f5(或者Column_0.Column_1.Column_2.Column_3)等命名而不是想要的具体对应字段命名 目录 解决问题 ...

  4. Python使用matplotlib可视化绘图时你可用的颜色有哪些?把色彩可视化出来自己选好了

    Python使用matplotlib可视化绘图时你可用的颜色有哪些?把色彩可视化出来自己选好了 目录 Python使用matplotlib可视化绘图时你可用的颜色有哪些?把色彩可视化出来自己选好了 # ...

  5. Python:matplotlib绘图时指定图像大小,放大图像

    matplotlib绘图时是默认的大小,有时候默认的大小会感觉图片里的内容都被压缩了,解决方法如下. 先是原始代码: from matplotlib import pyplot as pltplt.f ...

  6. 成功解决 利用plt.plot绘图时,横坐标出现浮点小数而不是整数的情况(坐标轴刻度)

    成功解决 利用plt.plot绘图时,横坐标出现浮点小数而不是整数的情况(坐标轴刻度) 目录 解决问题 解决方法 相关文章 成功解决matplotlib绘图的时候,自定义横坐标和纵坐标刻度数值(调整坐 ...

  7. 154在屏幕中绘图时设置透明度(扩展知识:为图片视图添加点击手势识别器,来实现点击事件操作)...

    一张图片,通过混合模式绘制后,能得到不同效果的图片. 这里的示例仅是测试效果:实际上可以通过不同程度的混合模式绘制,来得到符合需求的效果. 效果如下: ViewController.h 1 #impo ...

  8. Python:使用matplotlib进行绘图时中文变成乱码的解决

    QUESTION:Python:使用matplotlib进行绘图时中文变成乱码的解决? ANSWER: 在绘图前加上下面的代码: plt.rcParams['font.sans-serif']=['S ...

  9. python画图显示不了中文_完美解决Python matplotlib绘图时汉字显示不正常的问题

    Matplotlib是一个很好的作图软件,但是python下默认不支持中文,所以需要做一些修改,方法如下: 1.在python安装目录的Lib目录下创建ch.py文件. 文件中代码为: 保存,以后通过 ...

  10. Python+matplotlib绘图时显示中文的设置方法

    封面图片:<Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社 图书详情: =================== 在使用Python+mat ...

最新文章

  1. 怎么样才可批量导出word文档中的照片 不再一张一张的另存为!
  2. 深入解析windows XP/2003:内核模式和用户模式
  3. 0228互联网新闻 | 抖音新推营销产品“抖店”;iTutorGroup牵手机器人索菲亚开创“新双师”模式...
  4. JDBC的CRUD操作之PreparedStatement的修改操作
  5. php三个数字比较大小排序,php中常用的4种实现数字大小排序的冒泡选择等算法函数代码...
  6. OpenCASCADE绘制测试线束:拓扑命令之纹理映射到形状
  7. 教程 打造OS X Mavericks原版 EFI Clover 引导安装
  8. Visio矢量图导出教程
  9. 10行代码AC——7-2 说反话-加强版 (20 分)——解题报告
  10. 芝麻信用分750以上有什么特殊作用?
  11. 数据结构与算法--实现Singleton模式
  12. 【机器学习】图片中的人脸识别
  13. 从托管到原生,MPP架构数据仓库的云原生实践
  14. 支持OpenStack,红帽将开源进行到底
  15. android--------Popupwindow的使用
  16. 思科的雾计算对物联网有何推助力?
  17. WebForm 基础
  18. CAD 绘图如何有效的控制精度?
  19. 最新版苹果手机html5摇一摇,iphone微信摇一摇失效问题解决
  20. LVDS 显示屏的两种接口标准:VESA 和 JEIDA;单/双路 LVDS 信号

热门文章

  1. appium之微信公众号自动化测试实战
  2. 电脑如何清理重复文件,查找电脑重复文件的软件
  3. 向安装包中添加设备 UDID. 蒲公英内测
  4. STM32笔记之 PWM(脉宽调制)
  5. mybatis中获取当前时间_MySQL NOW和SYSDATE函数:获取当前时间日期
  6. 【华为云技术分享】Linux内核发展史 (1)
  7. 路由跟踪在线Tracert工具说明
  8. Java 简单论文查重程序(SimHash+海明距离算法)
  9. 双系统还是虚拟机 linux系统时间,mac装双系统好还是虚拟机好_mac装虚拟机好还是双系统-系统城...
  10. 游戏对战平台研究终结篇