XGBoost学习(一):原理
XGBoost学习(二):安装及介绍
XGBoost学习(三):模型详解
XGBoost学习(四):实战
XGBoost学习(五):参数调优
XGBoost学习(六):输出特征重要性以及筛选特征
完整代码及其数据

Xgboost实战

Xgboost有两大类接口:Xgboost原生接口 和sklearn接口,并且Xgboost能够实现分类回归两种任务。下面对这四种情况做以解析。

1,基于Xgboost原生接口的分类

from sklearn.datasets import load_iris
import xgboost as xgb
from xgboost import plot_importance
import matplotlib.pyplot  as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score  # 准确率# 记载样本数据集
iris = load_iris()
X,y = iris.data,iris.target
# 数据集分割
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=123457)# 算法参数
params = {'booster':'gbtree','objective':'multi:softmax','num_class':3,'gamma':0.1,'max_depth':6,'lambda':2,'subsample':0.7,'colsample_bytree':0.7,'min_child_weight':3,'slient':1,'eta':0.1,'seed':1000,'nthread':4,
}plst = params.items()# 生成数据集格式
dtrain = xgb.DMatrix(X_train,y_train)
num_rounds = 500
# xgboost模型训练
model = xgb.train(plst,dtrain,num_rounds)# 对测试集进行预测
dtest = xgb.DMatrix(X_test)
y_pred = model.predict(dtest)# 计算准确率
accuracy = accuracy_score(y_test,y_pred)
print('accuarcy:%.2f%%'%(accuracy*100))# 显示重要特征
plot_importance(model)
plt.show()

输出预测正确率以及特征重要性:
accuarcy:93.33%

2,基于Xgboost原生接口的回归

import xgboost as xgb
from xgboost import plot_importance
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from sklearn.metrics import mean_squared_error# 加载数据集,此数据集时做回归的
boston = load_boston()
X,y = boston.data,boston.target# Xgboost训练过程
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)# 算法参数
params = {'booster':'gbtree','objective':'reg:gamma','gamma':0.1,'max_depth':5,'lambda':3,'subsample':0.7,'colsample_bytree':0.7,'min_child_weight':3,'slient':1,'eta':0.1,'seed':1000,'nthread':4,
}dtrain = xgb.DMatrix(X_train,y_train)
num_rounds = 300
plst = params.items()
model = xgb.train(plst,dtrain,num_rounds)# 对测试集进行预测
dtest = xgb.DMatrix(X_test)
ans = model.predict(dtest)# 显示重要特征
plot_importance(model)
plt.show()

重要特征(值越大,说明该特征越重要)显示结果:

3,Xgboost使用sklearn接口的分类(推荐)
XGBClassifier

from xgboost.sklearn import XGBClassifier
clf = XGBClassifier(silent=0,  # 设置成1则没有运行信息输出,最好是设置为0,是否在运行升级时打印消息# nthread = 4  # CPU 线程数 默认最大learning_rate=0.3 , # 如同学习率min_child_weight = 1,# 这个参数默认为1,是每个叶子里面h的和至少是多少,对正负样本不均衡时的0-1分类而言# 假设h在0.01附近,min_child_weight为1 意味着叶子节点中最少需要包含100个样本# 这个参数非常影响结果,控制叶子节点中二阶导的和的最小值,该参数值越小,越容易过拟合max_depth=6, # 构建树的深度,越大越容易过拟合gamma = 0,# 树的叶子节点上做进一步分区所需的最小损失减少,越大越保守,一般0.1 0.2这样子subsample=1, # 随机采样训练样本,训练实例的子采样比max_delta_step=0,  # 最大增量步长,我们允许每个树的权重估计colsample_bytree=1, # 生成树时进行的列采样reg_lambda=1, #控制模型复杂度的权重值的L2正则化项参数,参数越大,模型越不容易过拟合# reg_alpha=0, # L1正则项参数# scale_pos_weight =1 # 如果取值大于0的话,在类别样本不平衡的情况下有助于快速收敛,平衡正负权重# objective = 'multi:softmax', # 多分类问题,指定学习任务和响应的学习目标# num_class = 10,  # 类别数,多分类与multisoftmax并用n_estimators=100,  # 树的个数seed = 1000,  # 随机种子# eval_metric ='auc'
)

基于Sckit-learn接口的分类

from sklearn.datasets import load_iris
import xgboost as xgb
from xgboost import plot_importance
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score# 加载样本数据集
iris = load_iris()
X,y = iris.data,iris.target
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=12343)# 训练模型
model = xgb.XGBClassifier(max_depth=5,learning_rate=0.1,n_estimators=160,silent=True,objective='multi:softmax')
model.fit(X_train,y_train)# 对测试集进行预测
y_pred = model.predict(X_test)#计算准确率
accuracy = accuracy_score(y_test,y_pred)
print('accuracy:%2.f%%'%(accuracy*100))# 显示重要特征
plot_importance(model)
plt.show()

输出结果:
accuracy:93%

4,基于Scikit-learn接口的回归

import xgboost as xgb
from xgboost import plot_importance
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston# 导入数据集
boston = load_boston()
X ,y = boston.data,boston.target# Xgboost训练过程
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)model = xgb.XGBRegressor(max_depth=5,learning_rate=0.1,n_estimators=160,silent=True,objective='reg:gamma')
model.fit(X_train,y_train)# 对测试集进行预测
ans = model.predict(X_test)# 显示重要特征
plot_importance(model)
plt.show()


根据其在输入数组中的索引,特征被自动命名为f0-f7。在问题描述中手动的将这些索引映射到名称,我们可以看到,F5(身体质量指数)具有最高的重要性,F3(皮肤折叠厚度)具有最低的重要性。

5,根据xgboost特征重要性得分进行特征选择

特征重要性得分,可以用于在scikit-learn中进行特征选择。通过SelectFromModel类实现,该类采用模型并将数据集转换为具有选定特征的子集。这个类可以采取预先训练的模型,例如在整个数据集上训练的模型。然后,它可以阈值来决定选择哪些特征。当在SelectFromModel实例上调用transform()方法时,该阈值被用于在训练集和测试集上一致性选择相同特征。

在下面的示例中,我们首先在训练集上训练xgboost模型,然后在测试上评估。使用从训练数据集计算的特征重要性,然后,将模型封装在一个SelectFromModel实例中。我们使用这个来选择训练集上的特征,用所选择的特征子集训练模型,然后在相同的特征方案下对测试集进行评估。

示例:

# select features using threshold
selection = SelectFromModel(model, threshold=thresh, prefit=True)
select_X_train = selection.transform(X_train)
# train model
selection_model = XGBClassifier()
selection_model.fit(select_X_train, y_train)
# eval model
select_X_test = selection.transform(X_test)
y_pred = selection_model.predict(select_X_test)

我们可以通过测试多个阈值,来从特征重要性中选择特征。具体而言,每个输入变量的特征重要性,本质上允许我们通过重要性来测试每个特征子集。

完整示例如下:

# use feature importance for feature selection
from numpy import loadtxt
from numpy import sort
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.feature_selection import SelectFromModel
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
Y = dataset[:,8]
# split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=7)
# fit model on all training data
model = XGBClassifier()
model.fit(X_train, y_train)
# make predictions for test data and evaluate
y_pred = model.predict(X_test)
predictions = [round(value) for value in y_pred]
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# Fit model using each importance as a threshold
thresholds = sort(model.feature_importances_)
for thresh in thresholds:# select features using thresholdselection = SelectFromModel(model, threshold=thresh, prefit=True)select_X_train = selection.transform(X_train)# train modelselection_model = XGBClassifier()selection_model.fit(select_X_train, y_train)# eval modelselect_X_test = selection.transform(X_test)y_pred = selection_model.predict(select_X_test)predictions = [round(value) for value in y_pred]accuracy = accuracy_score(y_test, predictions)print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" % (thresh, select_X_train.shape[1], accuracy*100.0))

运行示例,得到输出:

Accuracy: 77.95%
Thresh=0.071, n=8, Accuracy: 77.95%
Thresh=0.073, n=7, Accuracy: 76.38%
Thresh=0.084, n=6, Accuracy: 77.56%
Thresh=0.090, n=5, Accuracy: 76.38%
Thresh=0.128, n=4, Accuracy: 76.38%
Thresh=0.160, n=3, Accuracy: 74.80%
Thresh=0.186, n=2, Accuracy: 71.65%
Thresh=0.208, n=1, Accuracy: 63.78%
我们可以看到,模型的性能通常随着所选择的特征的数量而减少。在这一问题上,可以对测试集准确率和模型复杂度做一个权衡,例如选择4个特征,接受准确率从77.95%降到76.38%。这可能是对这样一个小数据集的清洗,但对于更大的数据集和使用交叉验证作为模型评估方案可能是更有用的策略。

6,整理代码1(原生XGB)

from sklearn.model_selection import train_test_split
from sklearn import metrics
from  sklearn.datasets  import  make_hastie_10_2
import xgboost as xgb
#记录程序运行时间
import time
start_time = time.time()
X, y = make_hastie_10_2(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)##test_size测试集合所占比例
#xgb矩阵赋值
xgb_train = xgb.DMatrix(X_train, label=y_train)
xgb_test = xgb.DMatrix(X_test,label=y_test)
##参数
params={
'booster':'gbtree',
'silent':1 ,#设置成1则没有运行信息输出,最好是设置为0.
#'nthread':7,# cpu 线程数 默认最大
'eta': 0.007, # 如同学习率
'min_child_weight':3,
# 这个参数默认是 1,是每个叶子里面 h 的和至少是多少,对正负样本不均衡时的 0-1 分类而言
#,假设 h 在 0.01 附近,min_child_weight 为 1 意味着叶子节点中最少需要包含 100 个样本。
#这个参数非常影响结果,控制叶子节点中二阶导的和的最小值,该参数值越小,越容易 overfitting。
'max_depth':6, # 构建树的深度,越大越容易过拟合
'gamma':0.1,  # 树的叶子节点上作进一步分区所需的最小损失减少,越大越保守,一般0.1、0.2这样子。
'subsample':0.7, # 随机采样训练样本
'colsample_bytree':0.7, # 生成树时进行的列采样
'lambda':2,  # 控制模型复杂度的权重值的L2正则化项参数,参数越大,模型越不容易过拟合。
#'alpha':0, # L1 正则项参数
#'scale_pos_weight':1, #如果取值大于0的话,在类别样本不平衡的情况下有助于快速收敛。
#'objective': 'multi:softmax', #多分类的问题
#'num_class':10, # 类别数,多分类与 multisoftmax 并用
'seed':1000, #随机种子
#'eval_metric': 'auc'
}
plst = list(params.items())
num_rounds = 100 # 迭代次数
watchlist = [(xgb_train, 'train'),(xgb_test, 'val')]#训练模型并保存
# early_stopping_rounds 当设置的迭代次数较大时,early_stopping_rounds 可在一定的迭代次数内准确率没有提升就停止训练
model = xgb.train(plst, xgb_train, num_rounds, watchlist,early_stopping_rounds=100,pred_margin=1)
#model.save_model('./model/xgb.model') # 用于存储训练出的模型
print "best best_ntree_limit",model.best_ntree_limit
y_pred = model.predict(xgb_test,ntree_limit=model.best_ntree_limit)
print ('error=%f' % (  sum(1 for i in range(len(y_pred)) if int(y_pred[i]>0.5)!=y_test[i]) /float(len(y_pred))))
#输出运行时长
cost_time = time.time()-start_time
print "xgboost success!",'\n',"cost time:",cost_time,"(s)......"

7,整理代码2(XGB使用sklearn)

from sklearn.model_selection import train_test_split
from sklearn import metrics
from  sklearn.datasets  import  make_hastie_10_2
from xgboost.sklearn import XGBClassifier
X, y = make_hastie_10_2(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)##test_size测试集合所占比例
clf = XGBClassifier(
silent=0 ,#设置成1则没有运行信息输出,最好是设置为0.是否在运行升级时打印消息。
#nthread=4,# cpu 线程数 默认最大
learning_rate= 0.3, # 如同学习率
min_child_weight=1,
# 这个参数默认是 1,是每个叶子里面 h 的和至少是多少,对正负样本不均衡时的 0-1 分类而言
#,假设 h 在 0.01 附近,min_child_weight 为 1 意味着叶子节点中最少需要包含 100 个样本。
#这个参数非常影响结果,控制叶子节点中二阶导的和的最小值,该参数值越小,越容易 overfitting。
max_depth=6, # 构建树的深度,越大越容易过拟合
gamma=0,  # 树的叶子节点上作进一步分区所需的最小损失减少,越大越保守,一般0.1、0.2这样子。
subsample=1, # 随机采样训练样本 训练实例的子采样比
max_delta_step=0,#最大增量步长,我们允许每个树的权重估计。
colsample_bytree=1, # 生成树时进行的列采样
reg_lambda=1,  # 控制模型复杂度的权重值的L2正则化项参数,参数越大,模型越不容易过拟合。
#reg_alpha=0, # L1 正则项参数
#scale_pos_weight=1, #如果取值大于0的话,在类别样本不平衡的情况下有助于快速收敛。平衡正负权重
#objective= 'multi:softmax', #多分类的问题 指定学习任务和相应的学习目标
#num_class=10, # 类别数,多分类与 multisoftmax 并用
n_estimators=100, #树的个数
seed=1000 #随机种子
#eval_metric= 'auc'
)
clf.fit(X_train,y_train,eval_metric='auc')
#设置验证集合 verbose=False不打印过程
clf.fit(X_train, y_train,eval_set=[(X_train, y_train), (X_val, y_val)],eval_metric='auc',verbose=False)
#获取验证集合结果
evals_result = clf.evals_result()
y_true, y_pred = y_test, clf.predict(X_test)
print"Accuracy : %.4g" % metrics.accuracy_score(y_true, y_pred)
#回归
#m_regress = xgb.XGBRegressor(n_estimators=1000,seed=0)

XGBoost学习(四):实战-python3相关推荐

  1. python3人工智能网盘_《Python3入门人工智能掌握机器学习+深度学习提升实战能力》百度云网盘资源分享下载[MP4/5.77GB]...

    内容简介 本资源为<Python3入门人工智能掌握机器学习+深度学习提升实战能力>百度云网盘资源分享下载,具体看下文目录,格式为MP4/5.77GB.本资源已做压缩包处理,请勿直接在百度网 ...

  2. XGBoost学习(六):输出特征重要性以及筛选特征

    XGBoost学习(一):原理 XGBoost学习(二):安装及介绍 XGBoost学习(三):模型详解 XGBoost学习(四):实战 XGBoost学习(五):参数调优 XGBoost学习(六): ...

  3. 数据与广告系列十五:商业兴趣标签建模XGboost调优实战

    作者·黄崇远 『数据虫巢』 全文共8210字 题图ssyer.com " 聊聊商业兴趣标签建模,顺带学习下kaggle竞赛神器,快哉." 01 前言 我们来回想下,早在第是一篇&l ...

  4. XGBoost学习(五):参数调优

    XGBoost学习(一):原理 XGBoost学习(二):安装及介绍 XGBoost学习(三):模型详解 XGBoost学习(四):实战 XGBoost学习(五):参数调优 XGBoost学习(六): ...

  5. XGBoost入门及实战

    kaggle比赛必备算法XGBoost入门及实战 xgboost一直在kaggle竞赛江湖里被传为神器,它在对结构化数据的应用占据主导地位,是目前开源的最快最好的工具包,与常见的工具包算法相比速度提高 ...

  6. TensorFlow 2.0深度学习案例实战

    向AI转型的程序员都关注了这个号???????????? 机器学习AI算法工程   公众号:datayx 基于TensorFlow 2.0正式版, 理论与实战结合,非常适合入门学习! 这是一本面向人工 ...

  7. springboot做网站_Github点赞接近100k的SpringBoot学习教程+实战推荐!牛批!

    如果觉得看完文章对你有帮助记得点个赞,不要做收藏党.丢进收藏夹吃灰哦! 很明显的一个现象,除了一些老项目,现在 Java 后端项目基本都是基于 Spring Boot 进行开发,毕竟它这么好用以及天然 ...

  8. 在不同领域,大家用爬虫怎么盈利的-Java网络爬虫系统性学习与实战系列(4)

    在不同领域,大家用爬虫怎么盈利的-Java网络爬虫系统性学习与实战系列(4) 文章目录 概述 出行抢票软件 微博上的僵尸粉 电商比价/返利平台 社区抓取数据和内容 联系方式 系列文章地址: Java网 ...

  9. Elasticsearch 学习(二).实战使用

    Elasticsearch 学习(二).实战使用 参考:http://www.passjava.cn/#/01.PassJava/02.PassJava_Architecture/15.Elastic ...

最新文章

  1. 百度2011实习生招聘笔试题
  2. Java数据结构和算法:234树和外部存储
  3. html游戏源妈简单,最简单的HTML5游戏——贪吃蛇
  4. Java反射机制的基本概念与使用_Java进阶之reflection(反射机制)——反射概念与基础...
  5. c语言程序链接过程,C语言简明教程(二):C程序编译链接过程和实例对照详解...
  6. 学习jQuery之旅--新手必须知道的常用方法
  7. java序列化(六) - protostuff序列化
  8. oracle ORA-00054 资源正忙
  9. 微软宣布放弃收购雅虎
  10. 关于理财的一点点学习成果(先写这些吧)
  11. 腾讯手游助手选择不了服务器失败,腾讯手游助手启动模拟器失败的解决方法
  12. BLM业务战略规划的底层逻辑是什么?
  13. C/C++ Npcap包实现ARP欺骗
  14. 算法-贪心算法知识总结
  15. 01 - vulhub - ActiveMq - CVE-2015-5254
  16. 字符串String知识总结(上) 冲冲冲!!!
  17. Sencha Themer
  18. 安科瑞无线测温装置变电所配电柜测温-安科瑞张宇洁
  19. git与github的使用方法
  20. linux的cp指令

热门文章

  1. 番茄”为什么会凋谢?
  2. WinCE的cab文件
  3. 帮忙投票-2009年度全国儿童摄影展艺术展(第十期)江苏南京展区
  4. sqlite数据库加锁提交_怎样给sqlite数据库设定密码
  5. 【TC SRM 312 Div1 Level3】CheapestIsland(轮廓线DP)(最小表示法)
  6. 技术教程 网站开发 Asp
  7. [Line: 197] Ran out of memory allocating 1073741824 bytes with alignment 0 怎样让内存不够的电脑打开UE4.26
  8. NLP 如何评估词向量
  9. 英业达服务器进系统界面是怎样的,获得大品牌大订单英业达服务器前景大好
  10. 首次写爬虫!,requests库加beautifulsoup(美味汤)爬取学校教室课程表