XGBoost特征选择
1. 特征选择的思维导图
2. XGBoost特征选择算法
(1) XGBoost算法背景
2016年,陈天奇在论文《 XGBoost:A Scalable Tree Boosting System》中正式提出该算法。XGBoost的基本思想和GBDT相同,但是做了一些优化,比如二阶导数使损失函数更精准;正则项避免树过拟合;Block存储可以并行计算等。XGBoost具有高效、灵活和轻便的特点,在数据挖掘、推荐系统等领域得到广泛的应用。
(2) 算法原理
(3) 算法实现--python
- from sklearn.model_selection import train_test_split
- from sklearn import metrics
- import xgboost as xgb
- import matplotlib.pyplot as plt
- from sklearn.model_selection import GridSearchCV
- import pandas as pd, numpy as np
- import matplotlib as mpl
- # mpl.rcParams['font.sans-serif']=['FangSong']
- # mpl.rcParams['axes.unicode_minus']=False
- fpath = r".\processData\filter.csv"
- Dataset = pd.read_csv(fpath)
- x = Dataset.loc[:, "nAcid":"Zagreb"]
- y1 = Dataset.loc[:, "IC50_nM"]
- y2 = Dataset.loc[:, "pIC50"]
- names = x.columns
- names = list(names)
- key = list(range(0, len(names)))
- names_dict = dict(zip(key, names))
- names_dicts = pd.DataFrame([names_dict])
- x_train, x_test, y_train, y_test = train_test_split(x, y2, test_size=0.33, random_state=7)
- """
- max_depth:树的最大深度
- """
- model = xgb.XGBRegressor(max_depth=6, learning_rate=0.12, n_estimators=90, min_child_weight=6, objective="reg:gamma")
- model.fit(x_train, y_train)
- feature_important = model.feature_importances_
- rank_idx = np.argsort(feature_important)[::-1]
- rank_idx30 = rank_idx[:30]
- rank_names30 = names_dicts.loc[:, rank_idx30]
- label = rank_names30.values[0, :]
- path1 = r"Xgboost排名前30的特征.csv"
- pd.DataFrame(label).to_csv(path1, index=False)
- x_score = np.sort(feature_important)[::-1]
- path = r"Xgboost排名前30的得分.csv"
- pd.DataFrame(x_score[:30]).to_csv(path, index=False)
- # xgboost网格搜索调参
- gsCv = GridSearchCV(model,
- {'max_depth':list(range(3, 10, 1)),
- '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],
- 'min_child_weight':list(range(2, 8, 2)),
- 'n_estimators':list(range(10, 101, 10))})
- gsCv.fit(x_train, y_train)
- print(gsCv.best_params_)
- cv_results = pd.DataFrame(gsCv.cv_results_)
- path = r"paramRank.csv"
- cv_results.to_csv(path, index=False)
- # 可视化
- plt.figure()
- plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
- plt.xlabel("Feature")
- plt.ylabel("Feature Score")
- plt.title("Feature Importance")
- plt.savefig("Xgboost")
- # 可视化
- plt.figure()
- plt.barh(label[::-1], x_score[:30][::-1], 0.6, align='center')
- plt.grid(ls=':', color='gray', alpha=0.4)
- plt.title("Xgboost Feature Importance")
- # 添加数据标签
- # for a, b in enumerate(rf_score[:30][::-1]):
- # plt.text(b+0.1, a-0.6/2, '%s' % b, ha='center', va='bottom')
- plt.savefig("前30名特征")
- plt.show()
注意:该算法没有数据是不能运行的,需要做适当的修改,后面使用网格调参,找到最优参数。
(4) 算法可视化
XGBoost特征选择的更多相关文章
- xgboost 特征选择,筛选特征的正要性
import pandas as pd import xgboost as xgb import operator from matplotlib import pylab as plt def ce ...
- 从信用卡欺诈模型看不平衡数据分类(1)数据层面:使用过采样是主流,过采样通常使用smote,或者少数使用数据复制。过采样后模型选择RF、xgboost、神经网络能够取得非常不错的效果。(2)模型层面:使用模型集成,样本不做处理,将各个模型进行特征选择、参数调优后进行集成,通常也能够取得不错的结果。(3)其他方法:偶尔可以使用异常检测技术,IF为主
总结:不平衡数据的分类,(1)数据层面:使用过采样是主流,过采样通常使用smote,或者少数使用数据复制.过采样后模型选择RF.xgboost.神经网络能够取得非常不错的效果.(2)模型层面:使用模型 ...
- XGBoost、LightGBM的详细对比介绍
sklearn集成方法 集成方法的目的是结合一些基于某些算法训练得到的基学习器来改进其泛化能力和鲁棒性(相对单个的基学习器而言)主流的两种做法分别是: bagging 基本思想 独立的训练一些基学习器 ...
- Stacking:Catboost、Xgboost、LightGBM、Adaboost、RF etc
python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...
- Xgboost总结
从决策树.随机森林.GBDT最终到XGBoost,每个热门算法都不是孤立存在的,而是基于一系列算法的改进与优化.决策树算法简单易懂可解释性强,但是过拟合风险很大,应用场景有限:随机森林采用Baggin ...
- Python机器学习笔记:XgBoost算法
前言 1,Xgboost简介 Xgboost是Boosting算法的其中一种,Boosting算法的思想是将许多弱分类器集成在一起,形成一个强分类器.因为Xgboost是一种提升树模型,所以它是将许多 ...
- 机器学习-树模型理论(GDBT,xgboost,lightBoost,随机森林)
tree based ensemble algorithms 主要介绍以下几种ensemble的分类器(tree based algorithms) xgboost lightGBM: 基于决策树算法 ...
- 随机森林RF、XGBoost、GBDT和LightGBM的原理和区别
目录 1.基本知识点介绍 2.各个算法原理 2.1 随机森林 -- RandomForest 2.2 XGBoost算法 2.3 GBDT算法(Gradient Boosting Decision T ...
- RF/GBDT/XGBoost/LightGBM简单总结(完结)
这四种都是非常流行的集成学习(Ensemble Learning)方式,在本文简单总结一下它们的原理和使用方法. Random Forest(随机森林): 随机森林属于Bagging,也就是有放回抽样 ...
随机推荐
- [cf1491F]Magnets
首先,只需要找到一个有磁性的位置,就可以通过$n-1$次判断其余磁铁是否有磁性,因此也就是要在$\lfloor\log_{2}n\rfloor+1$次中找到一个有磁性的位置 有一个$n-1$次的做法, ...
- P7416 [USACO21FEB] No Time to Dry P
题目传送门 题意简述:给出颜色序列 \(a\),多次询问给出 \(l,r\),求涂成 \(a_l,a_{l+1},\cdots,a_r\) 的最小操作次数.每次涂色只能用一段数值更大的颜色覆盖原有的颜 ...
- 41-Climbing Stairs-leetcode
Climbing Stairs My Submissions QuestionEditorial Solution Total Accepted: 106498 Total Submissions: ...
- Qemu/kvm虚拟化源码解析学习视频资料
地址链接:tao宝搜索:Linux云计算KVM Qemu虚拟化视频源码讲解+实践https://item.taobao.com/item.htm?ft=t&id=646300730262 L ...
- keybd_event模拟键盘按键,mouse_event怎么用
从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...
- python生成器浅析
A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...
- Maven打包及场景
场景一 对当前项目打包并指定主类. <build> <plugins> <plugin> <artifactId>maven-compiler-plug ...
- 节省内存的循环banner(一)
循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...
- jenkins之代码部署回滚脚本
#!/bin/bash DATE=`date +%Y-%m-%d_%H-%M-%S` METHOD=$1 BRANCH=$2 GROUP_LIST=$3 function IP_list(){ if ...
- ORACLE 服务器验证
位于$ORACLE_HOME/network/admin/sqlnet.oraSQLNET.AUTHENTICATION_SERVICES=none|all|ntsnone:关闭操作系统认证,只能密码 ...