XGBoost使用教程(与sklearn一起使用)二
一、导入必要的工具包
# 运行 xgboost安装包中的示例程序
from xgboost import XGBClassifier
# 加载LibSVM格式数据模块
from sklearn.datasets import load_svmlight_file
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
二、数据读取
scikit-learn支持多种格式的数据,包括LibSVM格式数据
XGBoost可以加载libsvm格式的文本数据,libsvm的文件格式(稀疏特征)如下:
1 101:1.2 102:0.03
0 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个属性,比如形状、气味等等(加工成libsvm格式后变成了126维特征),
然后给出了这个蘑菇是否可食用。其中6513个样本做训练,1611个样本做测试。
数据下载地址:http://download.csdn.net/download/u011630575/10266113
# read in data,数据在xgboost安装的路径下的demo目录,现在copy到代码目录下的data目录
my_workpath = './data/'
X_train,y_train = load_svmlight_file(my_workpath + 'agaricus.txt.train')
X_test,y_test = load_svmlight_file(my_workpath + 'agaricus.txt.test')
print(X_train.shape)
print (X_test.shape)
三、训练参数设置
max_depth: 树的最大深度。缺省值为6,取值范围为:[1,∞]
eta:为了防止过拟合,更新过程中用到的收缩步长。在每次提升计算之后,算法会直接获得新特征的权重。
eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3,取值范围为:[0,1]
silent:取0时表示打印出运行时信息,取1时表示以缄默方式运行,不打印运行时信息。缺省值为0
objective: 定义学习任务及相应的学习目标,“binary:logistic” 表示二分类的逻辑回归问题,输出为概率。
其他参数取默认值。
四、训练模型
# 设置boosting迭代计算次数
num_round = 2
bst =XGBClassifier(max_depth=2, learning_rate=1, n_estimators=num_round,
silent=True, objective='binary:logistic') #sklearn api
bst.fit(X_train, y_train)
XGBoost预测的输出是概率。这里蘑菇分类是一个二类分类问题,输出值是样本为第一类的概率。
我们需要将概率值转换为0或1。
train_preds = bst.predict(X_train)
train_predictions = [round(value) for value in train_preds]
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
五、测试
模型训练好后,可以用训练好的模型对测试数据进行预测
XGBoost预测的输出是概率,输出值是样本为第一类的概率。我们需要将概率值转换为0或1。
# make prediction
preds = bst.predict(X_test)
predictions = [round(value) for value in preds]
test_accuracy = accuracy_score(y_test, predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
六、代码整理
# coding:utf-8
# 运行 xgboost安装包中的示例程序
from xgboost import XGBClassifier
# 加载LibSVM格式数据模块
from sklearn.datasets import load_svmlight_file
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
# read in data,数据在xgboost安装的路径下的demo目录,现在copy到代码目录下的data目录
my_workpath = './data/'
X_train,y_train = load_svmlight_file(my_workpath + 'agaricus.txt.train')
X_test,y_test = load_svmlight_file(my_workpath + 'agaricus.txt.test')
print(X_train.shape)
print(X_test.shape)
# 设置boosting迭代计算次数
num_round = 2
#bst = XGBClassifier(**params)
#bst = XGBClassifier()
bst =XGBClassifier(max_depth=2, learning_rate=1, n_estimators=num_round,
silent=True, objective='binary:logistic')
bst.fit(X_train, y_train)
train_preds = bst.predict(X_train)
train_predictions = [round(value) for value in train_preds]
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
# make prediction
preds = bst.predict(X_test)
predictions = [round(value) for value in preds]
test_accuracy = accuracy_score(y_test, predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
---------------------
作者:鹤鹤有明
来源:CSDN
原文:https://blog.csdn.net/u011630575/article/details/79421053
版权声明:本文为博主原创文章,转载请附上博文链接!
XGBoost使用教程(与sklearn一起使用)二的更多相关文章
- CG基础教程-陈惟老师十二讲笔记
转自 麽洋TinyOcean:http://www.douban.com/people/Tinyocean/notes?start=50&type=note 因为看了陈惟十二讲视频没有课件,边 ...
- JSTL标签库的基本教程之核心标签库(二)
JSTL标签库的基本教程之核心标签库(二) 核心标签库 标签 描述 <c:out> 用于在JSP中显示数据,就像<%= ... > <c:set> 用于保存数据 & ...
- Elasticsearch入门教程(六):Elasticsearch查询(二)
原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...
- XGBoost使用教程(纯xgboost方法)一
一.导入必要的工具包# 导入必要的工具包import xgboost as xgb # 计算分类正确率from sklearn.metrics import accuracy_score二.数据读取X ...
- XGBoost使用教程(进阶篇)三
一.Importing all the libraries import pandas as pdimport numpy as npfrom matplotlib import pyplot as ...
- Android快乐贪吃蛇游戏实战项目开发教程-03虚拟方向键(二)绘制一个三角形
该系列教程概述与目录:http://www.cnblogs.com/chengyujia/p/5787111.html 一.绘制三角形 在上一篇文章中,我们已经新建了虚拟方向键的自定义控件Direct ...
- 《C#图解教程》读书笔记之二:存储、类型和变量
本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.类型初窥:掀起你的盖头来 (1)C程序是一组函数和数据类型,C++程序是一组函数和类,而C#程序是一组类型声 ...
- xgboost算法教程(两种使用方法)
标签: xgboost 作者:炼己者 ------ 欢迎大家访问我的简书以及我的博客 本博客所有内容以学习.研究和分享为主,如需转载,请联系本人,标明作者和出处,并且是非商业用途,谢谢! ------ ...
- sklearn常见分类器(二分类模板)
# -*- coding: utf-8 -*- import pandas as pd import matplotlib matplotlib.rcParams['font.sans-serif'] ...
随机推荐
- Linux 访问iphone 上的照片
下面的链接,就差测试安装ifuse 了,估计是没有什么问题的. https://www.dedoimedo.com/computers/fedora-22-iphone.html
- java基础JDK jvm path环境变量
JDk=JRE +java的开发工具(javac.exe java.exe javadoc.exe)JRE =JVM +Java核心类库 2.为什么 要配置 path环境变量 ?如何配置?JAVA_H ...
- [LeetCode] 174. Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Java后端传Long类型给前端导致的精度丢失
问题:实体属性是Long类型,在后端值本来是1119102511023023410,但是返回给前端的却是1119102511023023400 解决方案:添加序列化注解 import com.fast ...
- 推荐一款手机清理工具App悟空清理
推荐一款手机清理工具App悟空清理 1 介绍 悟空清理是一款完全免费的手机加速与存储空间清理工具软件,强力去除顽固垃圾,使手机运行更畅快. 2 特色功能介绍 悟空在手,清理无忧!悟空清理,人人都在用的 ...
- Using MS Soap toolkit to generate web services .md
Different SOAP encoding styles - RPC, RPC-literal, and document-literal SOAP Remote Procedure Call(R ...
- Java8 新特性 Stream() 创建流
通过Controllere类的Stream()和parallelStream()创建流 //通过集合创建流 @Test public void test1() { String arr[] = new ...
- C++:inline
inline inline是C++提供的一个关键字,它用于函数定义之前,表示把函数定义为内联函数.内联函数的含义是:在函数调用点把函数体直接展开,取代函数调用. inline int getZero( ...
- 2018 ACM-ICPC徐州站网络赛 G题
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xxx , yy ...
- python 属性描述符
import numbers class IntField: # 一个类只要实现了这个魔法函数,那么它就是属性描述符 #数据描述符 def __get__(self, instance, owner) ...