搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
小小林熬夜学编程
这个屌丝很懒,什么也没留下!
关注作者
热门标签
article
热门文章
1
Microsoft Visual C++ 14.0 is required解决方法_microsoft visual c++ 14.0离线安装
2
自学大语言模型的应用程序框架Langchain(初入门)_langchain应用框架
3
vue自定义抽屉组件_vue 抽屉组件 适用手机端
4
一文学会Axios的使用
5
STM32F4存储器映射 和 寄存器映射_stm32f4重映射
6
您知道用 Docker打包 Springboot有多少种方式?
7
kail-添加静态路由_kali添加路由命令
8
linux磁盘空间查看及空间满的处理,linux磁盘空间占满问题快速定位并解决
9
C语言:对称二叉树_对称二叉树c语言
10
AI之DL:人工智能领域—深度学习的发展历程之深度学习爆发的三大因素、探究DL为什么耗算力
当前位置:
article
> 正文
XGBoost解决多分类问题_多分类xgboost
作者:小小林熬夜学编程 | 2024-02-16 22:46:53
赞
踩
多分类xgboost
XGBoost
解决多分类问题
写在前面的话
XGBoost官方给的二分类问题的
例子
是区别蘑菇有无毒,数据集和代码都可以在xgboost中的demo文件夹对应找到,我是用的Anaconda安装的XGBoost,实现起来比较容易。唯一的梗就是在终端中运行所给命令: ../../xgboost mushroom.conf 时会报错,是路径设置的问题,所以我干脆把xgboost文件夹下的xgboost.exe拷到了mushroom.conf配置文件所在文件夹下,这样直接定位到该文件夹下就可以运行: xgboost mushroom.conf。二分类数据预处理,也就是data wraggling部分的代码有一定的借鉴意义,值得一看。
多分类问题给的
例子
是根据34个特征识别6种皮肤病,由于终端中运行runexp.sh没有反应,也不报错,所以我干脆把数据集下载到对应的demo文件夹下了,主要的代码如下,原来有部分比较难懂的语句我自己加了一些注释,这样理解起来就会顺畅多了。
[python]
view plain
copy
#! /usr/bin/python
import
numpy as np
import
xgboost as xgb
# label need to be 0 to num_class -1
# if col 33 is '?' let it be 1 else 0, col 34 substract 1
data = np.loadtxt(
'./dermatology.data'
, delimiter=
','
,converters={
33
:
lambda
x:int(x ==
'?'
),
34
:
lambda
x:int(x)-
1
} )
sz = data.shape
train = data[:int(sz[
0
] *
0.7
), :]
# take row 1-256 as training set
test = data[int(sz[
0
] *
0.7
):, :]
# take row 257-366 as testing set
train_X = train[:,
0
:
33
]
train_Y = train[:,
34
]
test_X = test[:,
0
:
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.1
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 );
print
(
'predicting, classification error=%f'
% (sum( int(pred[i]) != test_Y[i]
for
i
in
range(len(test_Y))) / float(len(test_Y)) ))
# 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)
yprob = bst.predict( xg_test ).reshape( test_Y.shape[
0
],
6
)
ylabel = np.argmax(yprob, axis=
1
)
# return the index of the biggest pro
print
(
'predicting, classification error=%f'
% (sum( int(ylabel[i]) != test_Y[i]
for
i
in
range(len(test_Y))) / float(len(test_Y)) ))
结果如下:
[python]
view plain
copy
[
0
] train-merror:
0.011719
test-merror:
0.127273
[
1
] train-merror:
0.015625
test-merror:
0.127273
[
2
] train-merror:
0.011719
test-merror:
0.109091
[
3
] train-merror:
0.007812
test-merror:
0.081818
[
4
] train-merror:
0.007812
test-merror:
0.090909
predicting, classification error=
0.090909
[
0
] train-merror:
0.011719
test-merror:
0.127273
[
1
] train-merror:
0.015625
test-merror:
0.127273
[
2
] train-merror:
0.011719
test-merror:
0.109091
[
3
] train-merror:
0.007812
test-merror:
0.081818
[
4
] train-merror:
0.007812
test-merror:
0.090909
predicting, classification error=
0.090909
不管是直接返回诊断类型,还是返回各类型的概率,然后取概率最大的那个对应的类型的index,结果都是一样的。
结语
强烈建议大家使用python notebook来实现代码,当有不明白的代码时看一下执行后的结果能帮助我们很快理解。同时要感叹一下,
看大神们的代码感觉好牛X,对我这个XGBoost paper看过两遍还没能完全领略算法精髓的人来说只能拿来主义了,希望后面有机会去读一读算法源码。
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/blog/article/detail/97431
推荐阅读
article
XGBoost
——
xgboost
算法_
xgboost
采用
hinge
目标
函数有什么好处相对于
svm
全栈工程师开发手册(作者:栾鹏)python数据挖掘系列教程安装
xgboost
目前还不能pip在线安装,所以先在网址https://www.lfd.uci.edu/~gohlke/pythonlibs/#
xgboost
中下载whl文件,然后...
[详细]
赞
踩
article
机器
学习
—
—
XGboost
原理及
python
实现_
python
xgboost
机器
学习
—
—
XGboost
原理及
python
实现_
python
xgboost
python
xgboost
XGboost
原理及实战原理1xgb是什么1.1CART回归树1.2应用1.3目标函数2xgb数学推导2.1回归树2.2加法模型2.3前向...
[详细]
赞
踩
article
【
项目
实战】Python实现
xgboost
分类
模型(
XGBClassifier
算法)
项目
实战_
python
xgboost
多
分类
说明:这是一个机器学习实战
项目
(附带数据+代码+文档+代码讲解),如需数据+代码+文档+代码讲解可以直接到文章最后获取。1.
项目
背景随着大数据时代的到来,具备大数据思想至关重要,人工智能技术在各行各业的应用已是随处可见。金融机构因车辆贷款违...
[详细]
赞
踩
article
XGBoost
的纯
Python
实现_
xgboost
python
在上一期的笔记中,我们主要讨论了
XGBoost
的原理,最近我在公众号机器学习实验室看到一篇文章,里面给出了
XGBoost
的纯
Python
实现,刚好能够作为补充,于是我将代码摘录了过来,方便学习和记录。_
xgboost
python
xgboos...
[详细]
赞
踩
article
XGBoost
—
—
机器学习(理论+图解+
安装
方法+
python
代码)_
xgboost
python
文章目录一瞥一、集成算法思想二、
XGBoost
基本思想三、MacOS
安装
XGBoost
四、用
python
实现
XGBoost
算法在竞赛题中经常会用到
XGBoost
算法,用这个算法通常会使我们模型的准确率有一个较大的提升。既然它效果这么好,那么...
[详细]
赞
踩
article
xgboost
python
分类
_
XGBoost
多
分类
预测
importpandasaspdfromsklearn.model_selectionimporttrain_test_splitfrom
xgboost
.sklearnimportXGBClassifierfromsklearn.metri...
[详细]
赞
踩
article
Spark
实现
xgboost
多分类(
python
)
_
spark
xgboost
1.
spark
-
xgboost
Java包主要需要
xgboost
4j-
spark
-0.90.jar,
xgboost
4j-0.90.jar,以及调用代码
spark
xgb.zip.GitHub上面有
xgboost
java实现的包,链接:xgboo...
[详细]
赞
踩
article
xgb
oost 多分类(六段
age
predict
)
_
xgb
多分类
1.相关包导入#-*-coding:utf-8-*-importnumpyasnpimport
xgb
oostas
xgb
from
xgb
oostimportplot
_
importancefromsklearnimportpreprocessin...
[详细]
赞
踩
article
python
平台
下
实现
xgboost
算法
及
输出
的
解释
_
python
xgboost
输出
每次迭代结果
python
平台
下
实现
xgboost
算法
及
输出
的
解释
问题描述数据集训练集与测试集Xgboost建模1模型初始化设置2建模与预测3可视化
输出
31得分32所属的叶子节点32特征重要性
python
平台
下
实现
xgboost
算法
及
输出
的
解释
1.问题描...
[详细]
赞
踩
article
【
XGBoost
多
分类
】
XGBoost
解决多
分类
问题
_
xgboost
多
分类
下面将以一个例子来讲解
XGBoost
解决多
分类
问题。1、下载数据集,数据集我们采用小麦种子数据集,该数据集有3类,已知小麦种子包含7个特征,分别为面积,周长,紧凑度,仔粒长度,仔粒宽度,不对称系数,仔粒腹沟长度,小麦类别为1,2,3linu...
[详细]
赞
踩
article
XGBoost
模型
参数
解释_
xgboost
.
predict
上篇博文介绍了
xgboost
这个算法的推导,下面我们在调包使用这个算法的时候,有一些
参数
是需要我们理解的。https://blog.csdn.net/weixin_43172660/article/details/83048394这是上篇博...
[详细]
赞
踩
article
xgboost
多分类:
objective
参数(reg:linear,
multi
:
softmax
,
multi
:
softprob
)对比分析_
multi
:
softmax
doesn
't
support
`
predict
_
proba
`. swi
一、问题上篇文章中我们已经搞明白了逻辑回归的问题中,
xgboost
的train(),fit()方法以及不同参数的
objective
的对应的输出结果和关系,这篇文章我们就来研究下
xgboost
再做多分类时,train(),fit()建模后输出...
[详细]
赞
踩
article
xgboost
实现多
分类
问题
demo
以及原理_
xgboost
demo
本文先把
xgboost
支持的多
分类
问题
的
demo
写起来,打印出生成的树结构,然后理解
xgboost
实现多
分类
问题
的原理。这个顺序比较好理解一些。
xgboost
多
分类
问题
demo
这个
demo
从
xgboost
的源代码中就可以看到。在/
demo
/...
[详细]
赞
踩
article
xgboost
:
predict
和
predict
_
proba
分类器
正确率
计算的讨论
_
xgboost
predict
_
proba
sklearn接口的
xgboost
分类器
:
xgboost
.XGBClaaifier()
xgboost
.fit(x,y)1.
xgboost
.
predict
(test
_
x)2.
xgboost
.
predict
_
proba
(test
_
x)3.xgb...
[详细]
赞
踩
article
XGBoost
.
predict
() TypeError:
predict
() got an
unexpected
keyword
argument
‘
data
‘_typeerror:
predict
_probability() got an
unexpected
@创建于:20211126文章目录1.问题描述2.解决办法3.原因3.1
XGBoost
==1.3.3的
predict
()接口3.2
XGBoost
==1.5.0的
predict
()接口1.问题描述利用
XGBoost
算法,训练好模型后,开展预测...
[详细]
赞
踩
article
XGBoost
Demo
importnumpyasnpimportpandasaspdimportxgboostasxgbfromsklearn.cross_validationimporttrain_test_splitimportosimportcsv#fro...
[详细]
赞
踩
article
不
平衡
处理:
xgboost
中
scale
_
pos
_
weight
、给
样本
设置
权重
weight
、 自定义损失函数 和 简单复制正
样本
的区别
在对不
平衡
数据进行训练时,通常会考虑一下怎么处理不
平衡
数据能使训练出来的结果较好。能想到的比较基础的方法是过采样和下采样来缓解数据中的正负
样本
比。在用
xgboost
训练二分类模型时,除了过采样和下采样,
xgboost
接口还提供一些处理不
平衡
...
[详细]
赞
踩
相关标签
机器学习
python
算法
xgboost分类模型
XGBClassifier算法
GridSearchCV
T检验卡方检验
深度学习
人工智能
xgboost python分类
xgboost
xgboost参数
多分类
softmax
树结构