首页 > 解决方案 > R:在 xgboost 中提取初始化预测

问题描述

library(xgboost)
data(agaricus.train, package='xgboost')
# Initialize baseline predictions to be 0
baseline_predictions <- rep(1.5, nrow(agaricus.train$data))
# base_margin is the base prediction Xgboost will boost from ;
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label, base_margin = baseline_predictions)
param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
              objective = "binary:logistic", eval_metric = "auc")
bst <- xgb.train(param, dtrain, nrounds = 2)
> xgb.dump(bst, with_stats = T)
 [1] "booster[0]"                                                                    
 [2] "0:[f28<-9.53674316e-07] yes=1,no=2,missing=1,gain=6691.7876,cover=971.39093"   
 [3] "1:[f55<-9.53674316e-07] yes=3,no=4,missing=3,gain=1923.16174,cover=551.54364"  
 [4] "3:leaf=0.742681563,cover=484.427734"                                           
 [5] "4:leaf=-4.93142509,cover=67.1159134"                                           
 [6] "2:[f108<-9.53674316e-07] yes=5,no=6,missing=5,gain=336.239258,cover=419.847321"
 [7] "5:leaf=-5.37396955,cover=411.942535"                                           
 [8] "6:leaf=1.08577335,cover=7.90476274"                                            
 [9] "booster[1]"                                                                    
[10] "0:[f59<-9.53674316e-07] yes=1,no=2,missing=1,gain=1517.97913,cover=354.008148" 
[11] "1:[f66<-9.53674316e-07] yes=3,no=4,missing=3,gain=1250.927,cover=340.298492"   
[12] "3:leaf=0.488599688,cover=338.470062"                                           
[13] "4:leaf=21.6099014,cover=1.82844138"                                            
[14] "2:leaf=-9.71027374,cover=13.709651"

在上面的代码中,我将训练数据中所有观察值的预测值初始化为 1.5 base_margin = baseline_predictions

使用xgb.dump,我可以看到适合的结果树。我的问题是,是否也可以提取初始预测?也就是说,给定一个 XGBoost 模型bst,我可以提取基线预测(即所有观察值为 1.5)?

标签: rmachine-learningxgboost

解决方案


这个问题的一个解决方案是使用xgboost::getinfo(object = dtrain, name = "base_margin")获取baseline_predictions。无论它们是预先设置在(例如,在本示例中设置为“1.5”)还是从初步训练运行中计算出baseline_predictions(例如在https://github.com/dmlc/xgboost/blob中),这都是有用的/master/R-package/demo/boost_from_prediction.R )


推荐阅读