1. 特征选择的思维导图

2. XGBoost特征选择算法

(1)  XGBoost算法背景

    2016年,陈天奇在论文《 XGBoost:A Scalable Tree Boosting System》中正式提出该算法。XGBoost的基本思想和GBDT相同,但是做了一些优化,比如二阶导数使损失函数更精准;正则项避免树过拟合;Block存储可以并行计算等。XGBoost具有高效、灵活和轻便的特点,在数据挖掘、推荐系统等领域得到广泛的应用。

  (2) 算法原理

  (3) 算法实现--python

  1. from sklearn.model_selection import train_test_split
  2. from sklearn import metrics
  3. import xgboost as xgb
  4. import matplotlib.pyplot as plt
  5. from sklearn.model_selection import GridSearchCV
  6. import pandas as pd, numpy as np
  7. import matplotlib as mpl
  8.  
  9. # mpl.rcParams['font.sans-serif']=['FangSong']
  10. # mpl.rcParams['axes.unicode_minus']=False
  11.  
  12. fpath = r".\processData\filter.csv"
  13. Dataset = pd.read_csv(fpath)
  14.  
  15. x = Dataset.loc[:, "nAcid":"Zagreb"]
  16. y1 = Dataset.loc[:, "IC50_nM"]
  17. y2 = Dataset.loc[:, "pIC50"]
  18.  
  19. names = x.columns
  20. names = list(names)
  21. key = list(range(0, len(names)))
  22. names_dict = dict(zip(key, names))
  23. names_dicts = pd.DataFrame([names_dict])
  24.  
  25. x_train, x_test, y_train, y_test = train_test_split(x, y2, test_size=0.33, random_state=7)
  26. """
  27. max_depth:树的最大深度
  28. """
  29. model = xgb.XGBRegressor(max_depth=6, learning_rate=0.12, n_estimators=90, min_child_weight=6, objective="reg:gamma")
  30. model.fit(x_train, y_train)
  31.  
  32. feature_important = model.feature_importances_
  33. rank_idx = np.argsort(feature_important)[::-1]
  34. rank_idx30 = rank_idx[:30]
  35.  
  36. rank_names30 = names_dicts.loc[:, rank_idx30]
  37. label = rank_names30.values[0, :]
  38. path1 = r"Xgboost排名前30的特征.csv"
  39. pd.DataFrame(label).to_csv(path1, index=False)
  40.  
  41. x_score = np.sort(feature_important)[::-1]
  42. path = r"Xgboost排名前30的得分.csv"
  43. pd.DataFrame(x_score[:30]).to_csv(path, index=False)
  44. # xgboost网格搜索调参
  45. gsCv = GridSearchCV(model,
  46. {'max_depth':list(range(3, 10, 1)),
  47. 'learning_rate':[0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2],
  48. 'min_child_weight':list(range(2, 8, 2)),
  49. 'n_estimators':list(range(10, 101, 10))})
  50.  
  51. gsCv.fit(x_train, y_train)
  52. print(gsCv.best_params_)
  53. cv_results = pd.DataFrame(gsCv.cv_results_)
  54. path = r"paramRank.csv"
  55. cv_results.to_csv(path, index=False)
  56.  
  57. # 可视化
  58. plt.figure()
  59. plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
  60. plt.xlabel("Feature")
  61. plt.ylabel("Feature Score")
  62. plt.title("Feature Importance")
  63. plt.savefig("Xgboost")
  64.  
  65. # 可视化
  66. plt.figure()
  67. plt.barh(label[::-1], x_score[:30][::-1], 0.6, align='center')
  68. plt.grid(ls=':', color='gray', alpha=0.4)
  69. plt.title("Xgboost Feature Importance")
  70. # 添加数据标签
  71. # for a, b in enumerate(rf_score[:30][::-1]):
  72. # plt.text(b+0.1, a-0.6/2, '%s' % b, ha='center', va='bottom')
  73.  
  74. plt.savefig("前30名特征")
  75. plt.show()

注意:该算法没有数据是不能运行的,需要做适当的修改,后面使用网格调参,找到最优参数。

   (4)   算法可视化

XGBoost特征选择的更多相关文章

  1. xgboost 特征选择,筛选特征的正要性

    import pandas as pd import xgboost as xgb import operator from matplotlib import pylab as plt def ce ...

  2. 从信用卡欺诈模型看不平衡数据分类(1)数据层面:使用过采样是主流,过采样通常使用smote,或者少数使用数据复制。过采样后模型选择RF、xgboost、神经网络能够取得非常不错的效果。(2)模型层面:使用模型集成,样本不做处理,将各个模型进行特征选择、参数调优后进行集成,通常也能够取得不错的结果。(3)其他方法:偶尔可以使用异常检测技术,IF为主

    总结:不平衡数据的分类,(1)数据层面:使用过采样是主流,过采样通常使用smote,或者少数使用数据复制.过采样后模型选择RF.xgboost.神经网络能够取得非常不错的效果.(2)模型层面:使用模型 ...

  3. XGBoost、LightGBM的详细对比介绍

    sklearn集成方法 集成方法的目的是结合一些基于某些算法训练得到的基学习器来改进其泛化能力和鲁棒性(相对单个的基学习器而言)主流的两种做法分别是: bagging 基本思想 独立的训练一些基学习器 ...

  4. Stacking:Catboost、Xgboost、LightGBM、Adaboost、RF etc

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  5. Xgboost总结

    从决策树.随机森林.GBDT最终到XGBoost,每个热门算法都不是孤立存在的,而是基于一系列算法的改进与优化.决策树算法简单易懂可解释性强,但是过拟合风险很大,应用场景有限:随机森林采用Baggin ...

  6. Python机器学习笔记:XgBoost算法

    前言 1,Xgboost简介 Xgboost是Boosting算法的其中一种,Boosting算法的思想是将许多弱分类器集成在一起,形成一个强分类器.因为Xgboost是一种提升树模型,所以它是将许多 ...

  7. 机器学习-树模型理论(GDBT,xgboost,lightBoost,随机森林)

    tree based ensemble algorithms 主要介绍以下几种ensemble的分类器(tree based algorithms) xgboost lightGBM: 基于决策树算法 ...

  8. 随机森林RF、XGBoost、GBDT和LightGBM的原理和区别

    目录 1.基本知识点介绍 2.各个算法原理 2.1 随机森林 -- RandomForest 2.2 XGBoost算法 2.3 GBDT算法(Gradient Boosting Decision T ...

  9. RF/GBDT/XGBoost/LightGBM简单总结(完结)

    这四种都是非常流行的集成学习(Ensemble Learning)方式,在本文简单总结一下它们的原理和使用方法. Random Forest(随机森林): 随机森林属于Bagging,也就是有放回抽样 ...

随机推荐

  1. [cf1491F]Magnets

    首先,只需要找到一个有磁性的位置,就可以通过$n-1$次判断其余磁铁是否有磁性,因此也就是要在$\lfloor\log_{2}n\rfloor+1$次中找到一个有磁性的位置 有一个$n-1$次的做法, ...

  2. P7416 [USACO21FEB] No Time to Dry P

    题目传送门 题意简述:给出颜色序列 \(a\),多次询问给出 \(l,r\),求涂成 \(a_l,a_{l+1},\cdots,a_r\) 的最小操作次数.每次涂色只能用一段数值更大的颜色覆盖原有的颜 ...

  3. 41-Climbing Stairs-leetcode

    Climbing Stairs My Submissions QuestionEditorial Solution Total Accepted: 106498 Total Submissions: ...

  4. Qemu/kvm虚拟化源码解析学习视频资料

    地址链接:tao宝搜索:Linux云计算KVM Qemu虚拟化视频源码讲解+实践​https://item.taobao.com/item.htm?ft=t&id=646300730262 L ...

  5. keybd_event模拟键盘按键,mouse_event怎么用

    从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...

  6. python生成器浅析

    A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...

  7. Maven打包及场景

    场景一 对当前项目打包并指定主类. <build> <plugins> <plugin> <artifactId>maven-compiler-plug ...

  8. 节省内存的循环banner(一)

    循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...

  9. jenkins之代码部署回滚脚本

    #!/bin/bash DATE=`date +%Y-%m-%d_%H-%M-%S` METHOD=$1 BRANCH=$2 GROUP_LIST=$3 function IP_list(){ if ...

  10. ORACLE 服务器验证

    位于$ORACLE_HOME/network/admin/sqlnet.oraSQLNET.AUTHENTICATION_SERVICES=none|all|ntsnone:关闭操作系统认证,只能密码 ...