如何在没有 XGBoost 库的情况下生成 XGBoost 输出?
How to produce XGBoost outputs without an XGBoost library?
我有一个在 Python 中训练的 XGBoost 二元分类器模型。
我想在不同的脚本环境 (MQL4) 中使用纯数学运算而不使用 XGBoost 库 (.predict) 从该模型生成新输入数据的输出。
任何人都可以帮助公式 and/or 算法吗?
经过一些逆向工程,我发现了方法;训练好模型后,首先将模型转储到文本文件中:
num_round = 3
bst = xgb.train( param, dtrain, num_round, watchlist )
bst.dump_model('D:/Python/classifyproduct.raw.txt')
然后为每个助推器找到输入特征集的叶概率。对所有这些概率求和,在我们的例子中,输入二元逻辑函数:
1/(1+exp(-sum))
这是针对给定输入特征集训练的 xgboost 模型的输出概率。例如,我的带有 2 个输入(a 和 b)文本文件的示例转储是:
booster[0]:
0:[b<-1] yes=1,no=2,missing=1
1:[a<0] yes=3,no=4,missing=3
3:[a<-2] yes=7,no=8,missing=7
7:leaf=0.522581
8:[b<-3] yes=13,no=14,missing=13
13:leaf=0.428571
14:leaf=-0.333333
4:leaf=-0.54
2:[a<2] yes=5,no=6,missing=5
5:[a<-8] yes=9,no=10,missing=9
9:leaf=-0.12
10:leaf=-0.56129
6:[b<2] yes=11,no=12,missing=11
11:leaf=-0.495652
12:[a<4] yes=15,no=16,missing=15
15:[b<7] yes=17,no=18,missing=17
17:leaf=-0.333333
18:leaf=0.333333
16:leaf=0.456
booster[1]:
0:[b<-1] yes=1,no=2,missing=1
1:[a<0] yes=3,no=4,missing=3
3:[b<-3] yes=7,no=8,missing=7
7:leaf=0.418665
8:[a<-3] yes=13,no=14,missing=13
13:leaf=0.334676
14:leaf=-0.282568
4:leaf=-0.424174
2:[a<2] yes=5,no=6,missing=5
5:[b<0] yes=9,no=10,missing=9
9:leaf=-0.048659
10:leaf=-0.445149
6:[b<2] yes=11,no=12,missing=11
11:leaf=-0.394495
12:[a<5] yes=15,no=16,missing=15
15:[b<7] yes=17,no=18,missing=17
17:leaf=-0.330064
18:leaf=0.333063
16:leaf=0.392826
booster[2]:
0:[b<-1] yes=1,no=2,missing=1
1:[a<0] yes=3,no=4,missing=3
3:[b<-3] yes=7,no=8,missing=7
7:leaf=0.356906
8:[a<-3] yes=13,no=14,missing=13
13:leaf=0.289085
14:leaf=-0.245992
4:leaf=-0.363819
2:[a<4] yes=5,no=6,missing=5
5:[a<2] yes=9,no=10,missing=9
9:[b<0] yes=15,no=16,missing=15
15:leaf=-0.0403689
16:leaf=-0.381402
10:[b<7] yes=17,no=18,missing=17
17:leaf=-0.307704
18:leaf=0.239974
6:[b<2] yes=11,no=12,missing=11
11:leaf=-0.308265
12:leaf=0.302142
我有 2 个特征作为输入。假设我们有 [4, 9] 作为输入。我们可以计算助推器概率为:
booster0 : 0.456
booster1 : 0.333063
booster2 : 0.302142
sum = 1.091205
1/(1+exp(-sum)) = 0.748608563
就是这样。
我知道这是一个旧线程,但万一有人看这里,有一个名为 m2cgen(模型到代码生成)的模块,它可以从模型生成纯本机代码,包括 xgboost(使用 gbtree booster) .
我有一个在 Python 中训练的 XGBoost 二元分类器模型。
我想在不同的脚本环境 (MQL4) 中使用纯数学运算而不使用 XGBoost 库 (.predict) 从该模型生成新输入数据的输出。
任何人都可以帮助公式 and/or 算法吗?
经过一些逆向工程,我发现了方法;训练好模型后,首先将模型转储到文本文件中:
num_round = 3
bst = xgb.train( param, dtrain, num_round, watchlist )
bst.dump_model('D:/Python/classifyproduct.raw.txt')
然后为每个助推器找到输入特征集的叶概率。对所有这些概率求和,在我们的例子中,输入二元逻辑函数:
1/(1+exp(-sum))
这是针对给定输入特征集训练的 xgboost 模型的输出概率。例如,我的带有 2 个输入(a 和 b)文本文件的示例转储是:
booster[0]:
0:[b<-1] yes=1,no=2,missing=1
1:[a<0] yes=3,no=4,missing=3
3:[a<-2] yes=7,no=8,missing=7
7:leaf=0.522581
8:[b<-3] yes=13,no=14,missing=13
13:leaf=0.428571
14:leaf=-0.333333
4:leaf=-0.54
2:[a<2] yes=5,no=6,missing=5
5:[a<-8] yes=9,no=10,missing=9
9:leaf=-0.12
10:leaf=-0.56129
6:[b<2] yes=11,no=12,missing=11
11:leaf=-0.495652
12:[a<4] yes=15,no=16,missing=15
15:[b<7] yes=17,no=18,missing=17
17:leaf=-0.333333
18:leaf=0.333333
16:leaf=0.456
booster[1]:
0:[b<-1] yes=1,no=2,missing=1
1:[a<0] yes=3,no=4,missing=3
3:[b<-3] yes=7,no=8,missing=7
7:leaf=0.418665
8:[a<-3] yes=13,no=14,missing=13
13:leaf=0.334676
14:leaf=-0.282568
4:leaf=-0.424174
2:[a<2] yes=5,no=6,missing=5
5:[b<0] yes=9,no=10,missing=9
9:leaf=-0.048659
10:leaf=-0.445149
6:[b<2] yes=11,no=12,missing=11
11:leaf=-0.394495
12:[a<5] yes=15,no=16,missing=15
15:[b<7] yes=17,no=18,missing=17
17:leaf=-0.330064
18:leaf=0.333063
16:leaf=0.392826
booster[2]:
0:[b<-1] yes=1,no=2,missing=1
1:[a<0] yes=3,no=4,missing=3
3:[b<-3] yes=7,no=8,missing=7
7:leaf=0.356906
8:[a<-3] yes=13,no=14,missing=13
13:leaf=0.289085
14:leaf=-0.245992
4:leaf=-0.363819
2:[a<4] yes=5,no=6,missing=5
5:[a<2] yes=9,no=10,missing=9
9:[b<0] yes=15,no=16,missing=15
15:leaf=-0.0403689
16:leaf=-0.381402
10:[b<7] yes=17,no=18,missing=17
17:leaf=-0.307704
18:leaf=0.239974
6:[b<2] yes=11,no=12,missing=11
11:leaf=-0.308265
12:leaf=0.302142
我有 2 个特征作为输入。假设我们有 [4, 9] 作为输入。我们可以计算助推器概率为:
booster0 : 0.456
booster1 : 0.333063
booster2 : 0.302142
sum = 1.091205
1/(1+exp(-sum)) = 0.748608563
就是这样。
我知道这是一个旧线程,但万一有人看这里,有一个名为 m2cgen(模型到代码生成)的模块,它可以从模型生成纯本机代码,包括 xgboost(使用 gbtree booster) .