将 XGBClassifier 模型转储到文本中
Dumping XGBClassifier model into text
我用 XGBBoost 训练了一个多标签class化模型,想在另一个系统中编写这个模型。
是否可以在 XGB Booster 中将我的 XGBClassifier 模型的文本输出视为 dump_model。
编辑:
我发现 model._Booster.dump_model(outputfile) returns 是一个转储文件,如下所示。但是,没有指定 class 的内容。在我的模型中,有 10 个 classes,但是在转储文件中只有一个助推器。所以,我不确定它是所有 classes 的模型还是其中一个。
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
树估计给定实例属于 class 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)
我用 XGBBoost 训练了一个多标签class化模型,想在另一个系统中编写这个模型。
是否可以在 XGB Booster 中将我的 XGBClassifier 模型的文本输出视为 dump_model。
编辑: 我发现 model._Booster.dump_model(outputfile) returns 是一个转储文件,如下所示。但是,没有指定 class 的内容。在我的模型中,有 10 个 classes,但是在转储文件中只有一个助推器。所以,我不确定它是所有 classes 的模型还是其中一个。
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
树估计给定实例属于 class 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)