在Python中使用XGBoost
2017-01-08 08:07
423 查看
转自:http://blog.csdn.net/zc02051126/article/details/46771793
python XGboost文档:http://xgboost.readthedocs.io/en/latest/python/python_api.html
下面将介绍XGBoost的Python模块,内容如下:
* 编译及导入Python模块
* 数据接口
* 参数设置
* 训练模型l
* 提前终止程序
* 预测
A walk through python example for UCI Mushroom dataset is provided.
首先安装XGBoost的C++版本,然后进入源文件的根目录下的
1
安装完成后按照如下方式导入XGBoost的Python模块
1
=
XGBoost可以加载libsvm格式的文本数据,加载的数据格式可以为Numpy的二维数组和XGBoost的二进制的缓存文件。加载的数据存储在对象
加载libsvm格式的数据和二进制的缓存文件时可以使用如下方式
2
1
2
加载numpy的数组到
2
3
1
2
3
将
2
1
2
将
2
1
2
可以用如下方式处理
1
当需要给样本设置权重时,可以用如下方式
2
1
2
XGBoost使用key-value格式保存参数. Eg
* Booster(基本学习器)参数
2
3
4
5
1
2
3
4
5
还可以定义验证数据集,验证算法的性能
1
=
有了参数列表和数据就可以训练模型了
* 训练
2
1
2
保存模型
在训练完成之后可以将模型保存下来,也可以查看模型内部的结构
1
Dump Model and Feature Map
You can dump the model to txt and review the meaning of model
2
3
4
1
2
3
4
加载模型
通过如下方式可以加载模型
2
1
2
=
如果有评价数据,可以提前终止程序,这样可以找到最优的迭代次数。如果要提前终止程序必须至少有一个评价数据在参数
The model will train until the validation score stops improving. Validation error needs to decrease at least every
continue training.
If early stopping occurs, the model will have two additional fields:
Note that
This works with both metrics to minimize (RMSE, log loss, etc.) and to maximize (MAP, NDCG, AUC).
=
After you training/loading a model and preparing the data, you can start to do prediction.
2
3
1
2
3
If early stopping is enabled during training, you can predict with the best iteration.
python XGboost文档:http://xgboost.readthedocs.io/en/latest/python/python_api.html
下面将介绍XGBoost的Python模块,内容如下:
* 编译及导入Python模块
* 数据接口
* 参数设置
* 训练模型l
* 提前终止程序
* 预测
A walk through python example for UCI Mushroom dataset is provided.
安装
首先安装XGBoost的C++版本,然后进入源文件的根目录下的 wrappers文件夹执行如下脚本安装Python模块
python setup.py install1
1
安装完成后按照如下方式导入XGBoost的Python模块
import xgboost as xgb1
1
=
数据接口
XGBoost可以加载libsvm格式的文本数据,加载的数据格式可以为Numpy的二维数组和XGBoost的二进制的缓存文件。加载的数据存储在对象DMatrix中。
加载libsvm格式的数据和二进制的缓存文件时可以使用如下方式
dtrain = xgb.DMatrix('train.svm.txt') dtest = xgb.DMatrix('test.svm.buffer')1
2
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
1
2
3
将
scipy.sparse格式的数据转化为
DMatrix格式时,可以使用如下方式
csr = scipy.sparse.csr_matrix( (dat, (row,col)) ) dtrain = xgb.DMatrix( csr )1
2
1
2
将
DMatrix格式的数据保存成XGBoost的二进制格式,在下次加载时可以提高加载速度,使用方式如下
dtrain = xgb.DMatrix('train.svm.txt') dtrain.save_binary("train.buffer")1
2
1
2
可以用如下方式处理
DMatrix中的缺失值:
dtrain = xgb.DMatrix( data, label=label, missing = -999.0)1
1
当需要给样本设置权重时,可以用如下方式
w = np.random.rand(5,1) dtrain = xgb.DMatrix( data, label=label, missing = -999.0, weight=w)1
2
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
1
2
3
4
5
还可以定义验证数据集,验证算法的性能
evallist = [(dtest,'eval'), (dtrain,'train')]1
1
=
训练模型
有了参数列表和数据就可以训练模型了 * 训练
num_round = 10 bst = xgb.train( plst, dtrain, num_round, evallist )1
2
1
2
保存模型
在训练完成之后可以将模型保存下来,也可以查看模型内部的结构
bst.save_model('0001.model')1
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
1
2
3
4
加载模型
通过如下方式可以加载模型
bst = xgb.Booster({'nthread':4}) #init model bst.load_model("model.bin") # load data1
2
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_roundsto
continue training.
If early stopping occurs, the model will have two additional fields:
bst.best_scoreand
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
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+python参数介绍的简单使用
- 【Python学习】python不要使用任何模块名作为文件名(module 'xgboost' has no attribute 'DMatrix')
- XGBoost:在Python中使用XGBoost
- windows(64位)下用GPU版本的mxnet配置(绑定python)+xgboost快速安装使用
- XGboost有关参数及其使用(PYTHON)
- XGBoost:在Python中使用XGBoost
- windows在python安装使用xgboost走了些弯路
- python包xgboost安装和简单使用
- python+xgboost在windows上的安装和使用
- XGBoost原理及在Python中使用XGBoost
- XGBoost:在Python中使用XGBoost
- machine learning in coding(python):使用xgboost构建预测模型
- 使用Boost.Python
- boost.python 使用
- boost.python vector_indexing_suite 使用注意事项
- 使用boost.python进行混合开发
- Boost.python 编译和使用
- C++使用boost python import 其他路径的模块
- 使用Boost.Python构建混合系统