mlr3 中的 XGBoost 树图

问题描述 投票:0回答:1

我正在尝试从适合 mlr3 的 XGboost 模型中获取

单树
的绘图,但我似乎找不到任何示例。我知道有一种方法可以使用底层
xgboost
(参见 here),即
mlr3
调用的方法,但我找不到任何方法可以从
mlr3
模型中提取树的结构。

举个例子:

#XGBoost tree plotting example

data(iris) #Iris dataset
sppNum <- as.numeric(iris$Species)-1 #Species as numeric (0-3), required for multiclass predictions

#Testing/training indices
testID <- as.vector(sapply(0:2,function(x) sort(sample(1:50,10,replace = FALSE)+(x*50)))) 
trainID <- which(!1:150 %in% testID)

# XGboost example ---------------------------------------------------------

library(xgboost)
library(DiagrammeR)
#Make testing/training datasets
datTrain <- xgb.DMatrix(data=as.matrix(iris[trainID,1:4]),label=sppNum[trainID]) 
datTest <- xgb.DMatrix(data=as.matrix(iris[testID,1:4]),label=sppNum[testID])
watchlist <- list(train = datTrain, eval = datTest)

#Parameter list
parList <- list(max_depth = 3, eta = 1, verbose = 0, nthread = 1,
                objective = "multi:softmax", eval_metric = "auc", num_class=3)
xgbMod <- xgb.train(parList, datTrain, nrounds = 100, watchlist) #Fit model

xgb.plot.tree(model = xgbMod, trees = 1) #Plot single tree

# mlr3 example ------------------------------------------------------------

library(mlr3)
library(mlr3learners)
library(mlr3viz)

data(iris)

tsk_mlr3 <- as_task_classif(iris,target='Species') #Set up task
#Set up learner
lrn_mlr3 <- lrn('classif.xgboost',nrounds=100,max_depth = 3, eta = 1,
                eval_metric='auc') 
lrn_mlr3$train(tsk_mlr3,row_ids = trainID) #Train learner on subset
lrn_mlr3$predict(tsk_mlr3,row_ids = testID) #Predict

xgb.plot.tree(lrn_mlr3$model, trees=1) #Doesn't work


有没有办法在

mlr3
中绘制(至少一棵)来自 XGboost 的树?

r plot xgboost mlr3
1个回答
0
投票

问题只是您需要指定

model=
此代码对我有用。

xgb.plot.tree(model = lrn_mlr3$model, trees=1)

我尝试了

debugonce(xgb.plot.tree)
,发现模型参数为NULL。

© www.soinside.com 2019 - 2024. All rights reserved.