XGBoost:在Python中使用XGBoost

原文:http://blog.csdn.net/zc02051126/article/details/46771793

在Python中使用XGBoost

下面将介绍XGBoost的Python模块,内容如下: 
编译及导入Python模块 
数据接口 
参数设置 
训练模型l 
提前终止程序 
预测

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‘, ‘[email protected]‘)]
  • 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)
时间: 2024-07-31 01:59:50

XGBoost:在Python中使用XGBoost的相关文章

Python中安装Xgboost(Windows)

网上的教程有很多,复杂且不一定能成功,造成了很大的困惑和时间成本,往往需要花费一上午或一下午的时间才能配置好环境,经过多次尝试,下面方法亲试有效: 下载 anaconda平台 https://www.anaconda.com/download/ 下载编译好的 DLL http://www.picnet.com.au/blogs/guido/2016/09/22/xgboost-windows-x64-binaries-for-download/ 下载git,克隆源码git clone  http

在Window平台下安装xgboost的Python版本

原文:http://blog.csdn.net/pengyulong/article/details/50515916 原文修改了两个地方才安装成功,第3步可以不用,第2步重新生成所有的就行了. 第4步,有“xgboost_wrapper.dll”以后,将该文件复制到/python-package/xgboost/中,继续后面步骤就可以了. 特别注意如果你的python是32位的,第二步就不要选择x64,而是选择win32.对应的文件也不是在x64下了.一定可以运行. xgboost的全称是eX

python环境下xgboost的安装与使用

xgboost是大规模并行boosted tree的工具,它是目前最快最好的开源boosted tree工具包,比常见的工具包快10倍以上.在数据科学方面,有大量kaggle选手选用它进行数据挖掘比赛,其中包括两个以上kaggle比赛的夺冠方案.在工业界规模方面,xgboost的分布式版本有广泛的可移植性,支持在YARN, MPI, Sungrid Engine等各个平台上面运行,并且保留了单机并行版本的各种优化,使得它可以很好地解决于工业界规模的问题. 本文就主要介绍一下xgboost在pyt

python模块安装(xgboost)

xgboost模块安装 1.下载xgboost源码 url:https://github.com/dmlc/xgboost/archive/master.zip 将压缩包剪切至python3\Scripts问价夹下进行解压(python的模块都在此文件夹下)  解压缩后的文件夹如下:xgboost-master > python-package > xgboost(确认能找到该条路径) 2.下载最新的dll文件 url:http://www.picnet.com.au/blogs/guido/

(转)XGBoost 与 Boosted Tree |《XGBoost 与 Boosted Tree | 我爱计算机》

//转自 <XGBoost 与 Boosted Tree | 我爱计算机> 1. 前言应 @龙星镖局  兄邀请写这篇文章.作为一个非常有效的机器学习方法,Boosted Tree是数据挖掘和机器学习中最常用的算法之一.因为它效果好,对于输入要求不敏感,往往是从统计学家到数据科学家必备的工具之一,它同时也是kaggle比赛冠军选手最常用的工具.最后,因为它的效果好,计算复杂度不高,也在工业界中有大量的应用. 2. Boosted Tree的若干同义词说到这里可能有人会问,为什么我没有听过这个名字

最新xgboost python32位下安装xgboost

网上很多windows python下安装xgboost都是很简单的几步无非是visual studio2013以上版本编译,安装.但现在最新的xgboost已经移除了c++工程文件,找到旧版本的也多是64位python版本安装xgboost的安装教程.由于我python32位已经安装了很多组件,改为64位过于麻烦.特搜索了一下完成以下教程. 前提:python已安装scipy numpy 1,下载旧版本的xgboost 提供两个网址,防止其中一个失效. http://download.csdn

走入计算机的第四十天(python中sockserver模块)

一.Python中的sockserver模块 1.该模块与sock模块不同之处是该模块自动帮我们分装好了一些功能,让我们在编程的时候直接调用这些功能就可以了,节省了编程步骤. 2.如图所示 注释:上图为服务端设置 该模块的操作方法比较死板,我们只要会熟悉的使用他就可以了.

python中if __name__ == &#39;__main__&#39;:

Using a module's __name__ Example? 8.2.? Using a module's __name__ #!/usr/bin/python # Filename: using_name.py if __name__ == '__main__': print 'This program is being run by itself' else: print 'I am being imported from another module' Output $ pytho

关于Python中的yield

关于Python中的yield http://www.cnblogs.com/tqsummer/archive/2010/12/27/1917927.html http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器是一个实现了迭代器协议