支付宝 收费 网站开发,北京页面设计制作,昆山网站设计,电子商务网站项目建设阶段的划分一、导入必要的工具包# 导入必要的工具包import xgboost as xgb# 计算分类正确率from sklearn.metrics import accuracy_score二、数据读取XGBoost可以加载libsvm格式的文本数据#xff0c;libsvm的文件格式(稀疏特征)如下#xff1a;1 101:1.2 102:0.030 1:2.1 10001:300 …一、导入必要的工具包# 导入必要的工具包import xgboost as xgb# 计算分类正确率from sklearn.metrics import accuracy_score二、数据读取XGBoost可以加载libsvm格式的文本数据libsvm的文件格式(稀疏特征)如下1 101:1.2 102:0.030 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.htmlXGBoost加载的数据存储在对象DMatrix中XGBoost自定义了一个数据矩阵类DMatrix优化了存储和运算速度DMatrix文档http://xgboost.readthedocs.io/en/latest/python/python_api.html数据下载地址http://download.csdn.net/download/u011630575/10266113# read in data数据在xgboost安装的路径下的demo目录,现在我们将其copy到当前代码下的data目录my_workpath ./data/dtrain xgb.DMatrix(my_workpath agaricus.txt.train)dtest xgb.DMatrix(my_workpath agaricus.txt.test)查看数据情况dtrain.num_col()dtrain.num_row()dtest.num_row()三、训练参数设置max_depth 树的最大深度。缺省值为6取值范围为[1,∞]eta为了防止过拟合更新过程中用到的收缩步长。在每次提升计算之后算法会直接获得新特征的权重。eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3取值范围为[0,1]silent取0时表示打印出运行时信息取1时表示以缄默方式运行不打印运行时信息。缺省值为0objective 定义学习任务及相应的学习目标“binary:logistic” 表示二分类的逻辑回归问题输出为概率。其他参数取默认值。# specify parameters via mapparam {max_depth:2, eta:1, silent:0, objective:binary:logistic }print(param)四、训练模型# 设置boosting迭代计算次数num_round 2import timestarttime time.clock()bst xgb.train(param, dtrain, num_round) # dtrain是训练数据集endtime time.clock()print (endtime - starttime)XGBoost预测的输出是概率。这里蘑菇分类是一个二类分类问题输出值是样本为第一类的概率。我们需要将概率值转换为0或1。train_preds bst.predict(dtrain)train_predictions [round(value) for value in train_preds]y_train dtrain.get_label() #值为输入数据的第一行train_accuracy accuracy_score(y_train, train_predictions)print (Train Accuary: %.2f%% % (train_accuracy * 100.0))五、测试模型训练好后可以用训练好的模型对测试数据进行预测# make predictionpreds bst.predict(dtest)检查模型在测试集上的正确率XGBoost预测的输出是概率输出值是样本为第一类的概率。我们需要将概率值转换为0或1。predictions [round(value) for value in preds]y_test dtest.get_label()test_accuracy accuracy_score(y_test, predictions)print(Test Accuracy: %.2f%% % (test_accuracy * 100.0))六、模型可视化调用XGBoost工具包中的plot_tree在显示要可视化模型需要安装graphviz软件包plot_tree()的三个参数1. 模型2. 树的索引从0开始3. 显示方向缺省为竖直‘LR是水平方向from matplotlib import pyplotimport graphvizxgb.plot_tree(bst, num_trees0, rankdir LR )pyplot.show()#xgb.plot_tree(bst,num_trees1, rankdir LR )#pyplot.show()#xgb.to_graphviz(bst,num_trees0)#xgb.to_graphviz(bst,num_trees1)七、代码整理# coding:utf-8import xgboost as xgb# 计算分类正确率from sklearn.metrics import accuracy_score# read in data数据在xgboost安装的路径下的demo目录,现在我们将其copy到当前代码下的data目录my_workpath ./data/dtrain xgb.DMatrix(my_workpath agaricus.txt.train)dtest xgb.DMatrix(my_workpath agaricus.txt.test)dtrain.num_col()dtrain.num_row()dtest.num_row()# specify parameters via mapparam {max_depth:2, eta:1, silent:0, objective:binary:logistic }print(param)# 设置boosting迭代计算次数num_round 2import timestarttime time.clock()bst xgb.train(param, dtrain, num_round) # dtrain是训练数据集endtime time.clock()print (endtime - starttime)train_preds bst.predict(dtrain) #print (train_preds,train_preds)train_predictions [round(value) for value in train_preds]print (train_predictions,train_predictions)y_train dtrain.get_label()print (y_train,y_train)train_accuracy accuracy_score(y_train, train_predictions)print (Train Accuary: %.2f%% % (train_accuracy * 100.0))# make predictionpreds bst.predict(dtest)predictions [round(value) for value in preds]y_test dtest.get_label()test_accuracy accuracy_score(y_test, predictions)print(Test Accuracy: %.2f%% % (test_accuracy * 100.0))# from matplotlib import pyplot# import graphvizimport graphviz# xgb.plot_tree(bst, num_trees0, rankdirLR)# pyplot.show()# xgb.plot_tree(bst,num_trees1, rankdir LR )# pyplot.show()# xgb.to_graphviz(bst,num_trees0)# xgb.to_graphviz(bst,num_trees1)