XGBoost:在Python中使用XGBoost
原文:http://blog.csdn.net/zc02051126/article/details/46771793
在Python中使用XGBoost
下面将介绍XGBoost的Python模块,内容如下:
* 编译及导入Python模块
* 数据接口
* 参数设置
* 训练模型l
* 提前终止程序
* 预测
A walk through python example for UCI Mushroom dataset is provided.
安装
首先安装XGBoost的C++版本,然后进入源文件的根目录下的 wrappers文件夹执行如下脚本安装Python模块
python setup.py install
- 1
安装完成后按照如下方式导入XGBoost的Python模块
import xgboost as xgb
- 1
=
数据接口
XGBoost可以加载libsvm格式的文本数据,加载的数据格式可以为Numpy的二维数组和XGBoost的二进制的缓存文件。加载的数据存储在对象DMatrix中。
- 加载libsvm格式的数据和二进制的缓存文件时可以使用如下方式
dtrain = xgb.DMatrix('train.svm.txt')
dtest = xgb.DMatrix('test.svm.buffer')
- 1
- 2
- 加载numpy的数组到
DMatrix对象时,可以用如下方式
data = np.random.rand(5,10) # 5 entities, each contains 10 features
label = np.random.randint(2, size=5) # binary target
dtrain = xgb.DMatrix( data, label=label)
- 1
- 2
- 3
- 将
scipy.sparse格式的数据转化为DMatrix格式时,可以使用如下方式
csr = scipy.sparse.csr_matrix( (dat, (row,col)) )
dtrain = xgb.DMatrix( csr )
- 1
- 2
- 将
DMatrix格式的数据保存成XGBoost的二进制格式,在下次加载时可以提高加载速度,使用方式如下
dtrain = xgb.DMatrix('train.svm.txt')
dtrain.save_binary("train.buffer")
- 1
- 2
- 可以用如下方式处理
DMatrix中的缺失值:
dtrain = xgb.DMatrix( data, label=label, missing = -999.0)
- 1
- 当需要给样本设置权重时,可以用如下方式
w = np.random.rand(5,1)
dtrain = xgb.DMatrix( data, label=label, missing = -999.0, weight=w)
- 1
- 2
参数设置
XGBoost使用key-value格式保存参数. Eg
* Booster(基本学习器)参数
param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' }
param['nthread'] = 4
plst = param.items()
plst += [('eval_metric', 'auc')] # Multiple evals can be handled in this way
plst += [('eval_metric', 'ams@0')]
- 1
- 2
- 3
- 4
- 5
- 还可以定义验证数据集,验证算法的性能
evallist = [(dtest,'eval'), (dtrain,'train')]
- 1
=
训练模型
有了参数列表和数据就可以训练模型了
* 训练
num_round = 10
bst = xgb.train( plst, dtrain, num_round, evallist )
- 1
- 2
- 保存模型
在训练完成之后可以将模型保存下来,也可以查看模型内部的结构
bst.save_model('0001.model')
- 1
- Dump Model and Feature Map
You can dump the model to txt and review the meaning of model
# dump model
bst.dump_model('dump.raw.txt')
# dump model with feature map
bst.dump_model('dump.raw.txt','featmap.txt')
- 1
- 2
- 3
- 4
- 加载模型
通过如下方式可以加载模型
bst = xgb.Booster({'nthread':4}) #init model
bst.load_model("model.bin") # load data
- 1
- 2
=
提前终止程序
如果有评价数据,可以提前终止程序,这样可以找到最优的迭代次数。如果要提前终止程序必须至少有一个评价数据在参数evals中。 If there’s more than one, it will use the last.
train(..., evals=evals, early_stopping_rounds=10)
The model will train until the validation score stops improving. Validation error needs to decrease at least every early_stopping_rounds to continue training.
If early stopping occurs, the model will have two additional fields: bst.best_score and bst.best_iteration. Note that train() will return a model from the last iteration, not the best one.
This works with both metrics to minimize (RMSE, log loss, etc.) and to maximize (MAP, NDCG, AUC).
=
Prediction
After you training/loading a model and preparing the data, you can start to do prediction.
data = np.random.rand(7,10) # 7 entities, each contains 10 features
dtest = xgb.DMatrix( data, missing = -999.0 )
ypred = bst.predict( xgmat )
- 1
- 2
- 3
If early stopping is enabled during training, you can predict with the best iteration.
ypred = bst.predict(xgmat,ntree_limit=bst.best_iteration)
XGBoost:在Python中使用XGBoost的更多相关文章
- XGBoost中参数调整的完整指南(包含Python中的代码)
(搬运)XGBoost中参数调整的完整指南(包含Python中的代码) AARSHAY JAIN, 2016年3月1日 介绍 如果事情不适合预测建模,请使用XGboost.XGBoost算法已 ...
- 机器学习——XGBoost大杀器,XGBoost模型原理,XGBoost参数含义
0.随机森林的思考 随机森林的决策树是分别采样建立的,各个决策树之间是相对独立的.那么,在我们得到了第k-1棵决策树之后,能否通过现有的样本和决策树的信息, 对第m颗树的建立产生有益的影响呢?在随机森 ...
- 在Window平台下安装xgboost的Python版本
原文:http://blog.csdn.net/pengyulong/article/details/50515916 原文修改了两个地方才安装成功,第3步可以不用,第2步重新生成所有的就行了. 第4 ...
- 一个完整的机器学习项目在Python中演练(四)
大家往往会选择一本数据科学相关书籍或者完成一门在线课程来学习和掌握机器学习.但是,实际情况往往d是,学完之后反而并不清楚这些技术怎样才能被用在实际的项目流程中.就像你的脑海中已经有了一块块" ...
- 最新xgboost python32位下安装xgboost
网上很多windows python下安装xgboost都是很简单的几步无非是visual studio2013以上版本编译,安装.但现在最新的xgboost已经移除了c++工程文件,找到旧版本的也多 ...
- rf, xgboost和GBDT对比;xgboost和lightGbm
1. RF 随机森林基于Bagging的策略是Bagging的扩展变体,概括RF包括四个部分:1.随机选择样本(放回抽样):2.随机选择特征(相比普通通bagging多了特征采样):3.构建决策树:4 ...
- [转]Python中的str与unicode处理方法
早上被python的编码搞得抓耳挠腮,在搜资料的时候感觉这篇博文很不错,所以收藏在此. python2.x中处理中文,是一件头疼的事情.网上写这方面的文章,测次不齐,而且都会有点错误,所以在这里打算自 ...
- python中的Ellipsis
...在python中居然是个常量 print(...) # Ellipsis 看别人怎么装逼 https://www.keakon.net/2014/12/05/Python%E8%A3%85%E9 ...
- python中的默认参数
https://eastlakeside.gitbooks.io/interpy-zh/content/Mutation/ 看下面的代码 def add_to(num, target=[]): tar ...
随机推荐
- 下拉框搜索插件chosen
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta ...
- CXF发布webservice
http://wenku.baidu.com/link?url=dTJpXcL0TXslGAYYC6SSOrPGvjyEb974ZGx9-0dymU32YDjxuP8DwlI1sFpPCGqu_ywW ...
- SQLSERVER2014集群实战——IP引发的坑
在之前的帖子里有提到过,为了避免IP变更带来的一系列问题,采取了不改变IP的策略.原以为,只要SQLSERVER的群集IP保持与之前单机部署的IP一致,就基本上不会有问题.然而实际永远比预想的更复杂. ...
- Codeforces.724G.Xor-matic Number of the Graph(线性基)
题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意 ...
- js取float型小数点后x位数的方法
js中取小数点后两位方法最常用的就是四舍五入函数了,前面我介绍过js中四舍五入一此常用函数,这里正好用上,下面我们一起来看取float型小数点后两位一些方法总结 以下我们将为大家介绍 JavaScri ...
- 指针式压力表自动读数:Auto Read the Value of Manometer
指针式压力表的自动读数,用摄像头对准压力计,然后实时自动地读取压力计的读数.视频效果如下视频所示,红色数字为识别到的指针读数.
- linux_jdk_mysql_tomcat
1 linux下安装jdk的步骤: 0. 查找原有的jdk: rpm -qa | grep java 删除原有的jdk: rpm -e --nodeps java-1.7.0-openjdk-1.7. ...
- JS字符串相关操作
01.插入 参数说明:str表示原字符串变量,flg表示要插入的字符串,sn表示要插入的位置 function insert_flg(str,flg,sn){ var newstr="&qu ...
- Iterative (non-recursive) Quick Sort
An iterative way of writing quick sort: #include <iostream> #include <stack> #include &l ...
- 【docker】docker部署spring boot项目在服务器上
IDE:idea 工具:docker spring boot:2.0.1 ======================================== 简单记录一下流程,以供参考: 第一步:首先得 ...