XGBOOST使用指南

XGBOOST使用指南

一、导入必要的工具包

  1. # 导入必要的工具包
  2. import xgboost as xgb
  3. # 计算分类正确率
  4. from sklearn.metrics import accuracy_score
二、数据读取
XGBoost可以加载libsvm格式的文本数据,libsvm的文件格式(稀疏特征)如下:
1  101:1.2 102:0.03
1:2.1 10001:300 10002:400
...
每一行表示一个样本,第一行的开头的“1”是样本的标签“101”和“102”为特征索引,'1.2'和'0.03' 为特征的值。

在两类分类中,用“1”表示正样本,用“0” 表示负样本。也支持[0,1]表示概率用来做标签,表示为正样本的概率。

下面的示例数据需要我们通过一些蘑菇的若干属性判断这个品种是否有毒。
UCI数据描述:http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/ ,
每个样本描述了蘑菇的22个属性,比如形状、气味等等(将22维原始特征用加工后变成了126维特征,

并存为libsvm格式),然后给出了这个蘑菇是否可食用。其中6513个样本做训练,1611个样本做测试。

注:libsvm格式文件说明如下 https://www.cnblogs.com/codingmengmeng/p/6254325.html

XGBoost加载的数据存储在对象DMatrix中
XGBoost自定义了一个数据矩阵类DMatrix,优化了存储和运算速度

DMatrix文档:http://xgboost.readthedocs.io/en/latest/python/python_api.html

数据下载地址:http://download.csdn.net/download/u011630575/10266113

  1. # read in data,数据在xgboost安装的路径下的demo目录,现在我们将其copy到当前代码下的data目录
  2. my_workpath = './data/'
  3. dtrain = xgb.DMatrix(my_workpath + 'agaricus.txt.train')
  4. dtest = xgb.DMatrix(my_workpath + 'agaricus.txt.test')

查看数据情况

dtrain.num_col()
dtrain.num_row()
dtest.num_row()
三、训练参数设置
  1. max_depth: 树的最大深度。缺省值为6,取值范围为:[1,∞]
  2. eta:为了防止过拟合,更新过程中用到的收缩步长。在每次提升计算之后,算法会直接获得新特征的权重。
  3. eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3,取值范围为:[0,1]
  4. silent:取0时表示打印出运行时信息,取1时表示以缄默方式运行,不打印运行时信息。缺省值为0
  5. objective: 定义学习任务及相应的学习目标,“binary:logistic” 表示二分类的逻辑回归问题,输出为概率。
  6. 其他参数取默认值。
  1. # specify parameters via map
  2. param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
  3. print(param)

四、训练模型

  1. # 设置boosting迭代计算次数
  2. num_round = 2
  3. import time
  4. starttime = time.clock()
  5. bst = xgb.train(param, dtrain, num_round) # dtrain是训练数据集
  6. endtime = time.clock()
  7. print (endtime - starttime)
XGBoost预测的输出是概率。这里蘑菇分类是一个二类分类问题,输出值是样本为第一类的概率。

我们需要将概率值转换为0或1。

  1. train_preds = bst.predict(dtrain)
  2. train_predictions = [round(value) for value in train_preds]
  3. y_train = dtrain.get_label() #值为输入数据的第一行
  4. train_accuracy = accuracy_score(y_train, train_predictions)
  5. print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))

五、测试

模型训练好后,可以用训练好的模型对测试数据进行预测

  1. # make prediction
  2. preds = bst.predict(dtest)
检查模型在测试集上的正确率

XGBoost预测的输出是概率,输出值是样本为第一类的概率。我们需要将概率值转换为0或1。

predictions = [round(value) for value in preds]
  1. y_test = dtest.get_label()
  2. test_accuracy = accuracy_score(y_test, predictions)
  3. print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))

六、模型可视化

调用XGBoost工具包中的plot_tree,在显示
要可视化模型需要安装graphviz软件包
plot_tree()的三个参数:
1. 模型
2. 树的索引,从0开始
3. 显示方向,缺省为竖直,‘LR'是水平方向

  1. from matplotlib import pyplot
  2. import graphviz
  3. xgb.plot_tree(bst, num_trees=0, rankdir= 'LR' )
  4. pyplot.show()
  5. #xgb.plot_tree(bst,num_trees=1, rankdir= 'LR' )
  6. #pyplot.show()
  7. #xgb.to_graphviz(bst,num_trees=0)
  8. #xgb.to_graphviz(bst,num_trees=1)

七、代码整理

  1. # coding:utf-8
  2. import xgboost as xgb
  3. # 计算分类正确率
  4. from sklearn.metrics import accuracy_score
  5. # read in data,数据在xgboost安装的路径下的demo目录,现在我们将其copy到当前代码下的data目录
  6. my_workpath = './data/'
  7. dtrain = xgb.DMatrix(my_workpath + 'agaricus.txt.train')
  8. dtest = xgb.DMatrix(my_workpath + 'agaricus.txt.test')
  9. dtrain.num_col()
  10. dtrain.num_row()
  11. dtest.num_row()
  12. # specify parameters via map
  13. param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
  14. print(param)
  15. # 设置boosting迭代计算次数
  16. num_round = 2
  17. import time
  18. starttime = time.clock()
  19. bst = xgb.train(param, dtrain, num_round) # dtrain是训练数据集
  20. endtime = time.clock()
  21. print (endtime - starttime)
  22. train_preds = bst.predict(dtrain) #
  23. print ("train_preds",train_preds)
  24. train_predictions = [round(value) for value in train_preds]
  25. print ("train_predictions",train_predictions)
  26. y_train = dtrain.get_label()
  27. print ("y_train",y_train)
  28. train_accuracy = accuracy_score(y_train, train_predictions)
  29. print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
  30. # make prediction
  31. preds = bst.predict(dtest)
  32. predictions = [round(value) for value in preds]
  33. y_test = dtest.get_label()
  34. test_accuracy = accuracy_score(y_test, predictions)
  35. print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
  36. # from matplotlib import pyplot
  37. # import graphviz
  38. import graphviz
  39. # xgb.plot_tree(bst, num_trees=0, rankdir='LR')
  40. # pyplot.show()
  41. # xgb.plot_tree(bst,num_trees=1, rankdir= 'LR' )
  42. # pyplot.show()
  43. # xgb.to_graphviz(bst,num_trees=0)
  44. # xgb.to_graphviz(bst,num_trees=1)



posted @ 2018-08-04 19:02  Expert668  阅读(508)  评论(0编辑  收藏  举报