python - 将 XGBClassifier 模型转储到文本中
问题描述
我用 XGBBoost 训练了一个多标签分类模型,并想在另一个系统中编写这个模型。
是否可以在 XGB Booster 中看到我的 XGBClassifier 模型的文本输出为 dump_model。
编辑:我发现 model._Booster.dump_model(outputfile) 返回如下转储文件。但是,没有指定类的任何内容。在我的模型中,有 10 个类,但是在转储文件中只有一个助推器。所以,我不确定它是所有类的模型还是其中一个。
booster[0]:
0:[101<0.142245024] yes=1,no=2,missing=1
1:[107<0.102833837] yes=3,no=4,missing=3
3:[101<0.039123565] yes=7,no=8,missing=7
7:leaf=-0.0142603116
8:leaf=0.023763923
4:[101<0.0646461397] yes=9,no=10,missing=9
9:leaf=-0.0345750563
10:leaf=-0.0135767004
2:[107<0.238691002] yes=5,no=6,missing=5
5:[103<0.0775454491] yes=11,no=12,missing=11
11:leaf=0.188941464
12:leaf=0.0651629418
6:[101<0.999929309] yes=13,no=14,missing=13
13:leaf=0.00403384864
14:leaf=0.236842111
booster[1]:
0:[102<0.014829753] yes=1,no=2,missing=1
1:[102<0.00999682024] yes=3,no=4,missing=3
3:[107<0.0966737345] yes=7,no=8,missing=7
7:leaf=-0.0387153365
8:leaf=-0.0486520194
4:[107<0.0922582299] yes=9,no=10,missing=9
9:leaf=0.0301927216
10:leaf=-0.0284226239
2:[102<0.199759275] yes=5,no=6,missing=5
5:[107<0.12201979] yes=11,no=12,missing=11
11:leaf=0.093562685
12:leaf=0.0127987256
6:[107<0.298737913] yes=13,no=14,missing=13
13:leaf=0.227570012
14:leaf=0.113037519
解决方案
查看示例数据集的源代码和输出,看起来好像第n
th 树估计了给定实例属于类 n 模 num_class 的可能性。我相信 xgboost 使用 softmax 函数,所以你想将树 i 的输出添加到weight[i%10]
然后取结果权重的 softmax。
booster_output(features, booster_index)
假设您有一个函数可以确定给定特征值的第 n 个增强树的输出,这样的事情应该可以工作:
import numpy as np
num_class = 10
num_boosters = 800
weight_of_classes = [0]*num_class
for i in range(num_boosters):
weight_of_classes[i%6] += booster_output(feature_values, i)
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()
probability_of_classes = softmax(weight_of_classes)
print(probability_of_classes)
推荐阅读
- powershell - 从powershell中的字符串搜索中提取整个单词
- python - DictReader 未在 python 3.8.2 中打印有序字典
- amazon-ec2 - 如何在 EC2 实例上安装 mdbtools?
- python - 无法使用 Flask_mail 发送电子邮件。拒绝连接
- mysql - 唯一键与 PK 的混淆
- c++ - vkDestroyDevice 返回 vkDevice has not been Destroyed 和 EXC_BAD_ACCESS
- javascript - React.js:搜索字符串以突出显示大括号内的文本 {..}
- swift - SwiftUI 和 RealityKit 的预览错误-“ARView”类型的值没有成员“raycast”
- ruby-on-rails - 升级的 ruby 仍然显示旧版本
- php - 如何在 PHP 7+ 和 Wordpress 5.4+ 版本中删除实时站点中的 php.ini 最大文件上传大小?