gpt4 book ai didi

python - 将 XGBClassifier 模型转储到文本中

转载 作者:太空狗 更新时间:2023-10-29 19:26:28 24 4
gpt4 key购买 nike

我使用 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 树估计给定实例属于类 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)

关于python - 将 XGBClassifier 模型转储到文本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com