eXtreme Gradient Boosting学习大杂烩

XGBoost简介:

XGBoost is an optimized distributed gradient boosting library designed to be highly efficient, flexible and portable. It implements machine learning algorithms under the Gradient Boosting framework. XGBoost provides a parallel tree boosting (also known as GBDT, GBM) that solve many data science problems in a fast and accurate way. The same code runs on major distributed environment (Hadoop, SGE, MPI) and can solve problems beyond billions of examples.

XGBoost是在Gradient Boosting框架下部署优化的机器学习算法的库,可以高效,精准解决大数据的一系列科学问题。

官方手册:https://xgboost.readthedocs.io/en/latest/

安装:

pip3 install xgboost

python部署示例:

import xgboost as xgb
# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' }
num_round = 2
bst = xgb.train(param, dtrain, num_round)
# make prediction
preds = bst.predict(dtest)

上面的例子,先读入train和test文件,然后进行训练,最后进行预测。首先,了解一下输入的数据格式。

基本输入数据格式:

XGBoost 支持两种输入格式:LibSVM and CSV

train.txt

1 101:1.2 102:0.03
0 1:2.1 10001:300 10002:400
0 0:1.3 1:0.3
1 0:0.01 1:0.3
0 0:0.2 1:0.3

每一行为一个独立输入,第一列为标签,‘101’,‘102’代表特征,其后紧随的'1.2'.'0.03'是特征值。

使用例程 multiclass_classification demo

通过训练,可以通过XGBoost实现对疾病的预测。

数据集信息:

这个数据库包含34属性, 其中33是线性值, 其中一个是标称的。

本组疾病为牛皮癣、seboreic 皮炎、扁平苔藓、糠、慢性皮炎、糠周角。通常活检是必要的诊断, 但不幸的是, 这些疾病也有许多病理学的特点。鉴别诊断的另一个难点是疾病可能在开始阶段显示另一种疾病的特征, 并可能在以下阶段具有特征性特征。首先对患者进行临床评价, 12 项特点。然后采用皮肤标本对22种病理学特征进行评价。病理学特征的值由显微镜下的样品分析确定。

在为该域构建的数据集中, 如果在家庭中观察到这些疾病中的任何一种, 则家族历史特征的值为 1, 否则为0。年龄特征仅仅代表病人的年龄。所有其他特征 (临床和病理学) 被给了程度在范围0到3。在这里, 0 表示该功能不存在, 3 表示可能的最大数量, 1, 2 表示相对中间值。

属性信息:

临床属性: (取值0、1、2、3, 除非另有说明)
1: 红斑
2: 缩放
3: 明确的边界
4: 发痒
5: koebner 现象
6: 多边形丘疹
7: 滤泡丘疹
8: 口腔黏膜介入
9: 膝和肘介入
10: 头皮介入
11: 家庭历史, (0 或 1)
34: 年龄 (线性)

病理学属性: (取值0、1、2、3)
12: 黑色素失禁
13: 嗜酸性粒细胞在渗透
14: 盈亏表现渗透
15: 乳突状真皮纤维化
16: 胞吐作用
17: 棘
18: 底角化过度
19: parakeratosis
20: 网脊的夜总会
21: 网脊的伸长率
22: suprapapillary 表皮的稀释
23: 海绵状脓包
24: 蒙罗 microabcess
25: 焦点 hypergranulosis
26: 微粒层数的消失
27: 基底层的 vacuolisation 和损伤
28: spongiosis
29: retes 的锯牙出现
30: 滤泡喇叭插头
31: perifollicular parakeratosis
32: 炎症 monoluclear inflitrate
33: 乐队象渗透

以上为数据格式,数据下载网址:https://archive.ics.uci.edu/ml/datasets/Dermatology

wget https://archive.ics.uci.edu/ml/machine-learning-databases/dermatology/dermatology.data

代码如下:

#!/usr/bin/python

from __future__ import division

import numpy as np
import xgboost as xgb

# label need to be 0 to num_class -1
data = np.loadtxt('./dermatology.data', delimiter=',',
        converters={33: lambda x:int(x == '?'), 34: lambda x:int(x) - 1})
sz = data.shape
#取前70%作为训练,后30%作为测试
train = data[:int(sz[0] * 0.7), :]
test = data[int(sz[0] * 0.7):, :]
#共33个特征
train_X = train[:, :33]
train_Y = train[:, 34]

test_X = test[:, :33]
test_Y = test[:, 34]

xg_train = xgb.DMatrix(train_X, label=train_Y)
xg_test = xgb.DMatrix(test_X, label=test_Y)
# setup parameters for xgboost
param = {}
# use softmax multi-class classification
param['objective'] = 'multi:softmax'
# scale weight of positive examples
param['eta'] = 0.5
param['max_depth'] = 6
param['silent'] = 1
param['nthread'] = 4
param['num_class'] = 6

watchlist = [(xg_train, 'train'), (xg_test, 'test')]
num_round = 5
bst = xgb.train(param, xg_train, num_round, watchlist)
# get prediction
pred = bst.predict(xg_test)
error_rate = np.sum(pred != test_Y) / test_Y.shape[0]
print('Test error using softmax = {}'.format(error_rate))

# do the same thing again, but output probabilities
param['objective'] = 'multi:softprob'
bst = xgb.train(param, xg_train, num_round, watchlist)
# Note: this convention has been changed since xgboost-unity
# get prediction, this is in 1D array, need reshape to (ndata, nclass)
pred_prob = bst.predict(xg_test).reshape(test_Y.shape[0], 6)
pred_label = np.argmax(pred_prob, axis=1)
error_rate = np.sum(pred_label != test_Y) / test_Y.shape[0]
print('Test error using softprob = {}'.format(error_rate))

运行结果:

误差计算:错误预测/测试集总数
error_rate = np.sum(pred_label != test_Y) / test_Y.shape[0]

测试案例预测输出,代表对一组数据进行各类预测后得出的概率:

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值