更改 Caret 在 R 中创建的图中显示的调整参数

Change tuning parameters shown in the plot created by Caret in R

我正在使用 R 中的 Caret 包通过 R 中称为 'xgbTree' 的方法训练模型。

绘制训练模型后如下图所示:调整参数即 'eta' = 0.2 不是我想要的,因为我也有 eta = 0.1 作为之前在 expand.grid 中定义的调整参数训练模型,这是最好的调子。所以我想将图中的 eta = 0.2 更改为 plot 函数中的 eta = 0.1 的场景。我该怎么做?谢谢你。

set.seed(100)  # For reproducibility

xgb_trcontrol = trainControl(
method = "cv",
#repeats = 2,
number = 10,  
#search = 'random',
allowParallel = TRUE,
verboseIter = FALSE,
returnData = TRUE
)


xgbGrid <- expand.grid(nrounds = c(100,200,1000),  # this is n_estimators in the python code above
                   max_depth = c(6:8),
                   colsample_bytree = c(0.6,0.7),
                   ## The values below are default values in the sklearn-api. 
                   eta = c(0.1,0.2),
                   gamma=0,
                   min_child_weight = c(5:8),
                   subsample = c(0.6,0.7,0.8,0.9)
)


set.seed(0) 
xgb_model8 = train(
x, y_train,  
trControl = xgb_trcontrol,
tuneGrid = xgbGrid,
method = "xgbTree"
)

发生的事情是绘图设备绘制了网格的所有值,最后出现的是 eta=0.2。例如:

xgb_trcontrol = trainControl(method = "cv", number = 3,returnData = TRUE)

xgbGrid <- expand.grid(nrounds = c(100,200,1000),  
                   max_depth = c(6:8),
                   colsample_bytree = c(0.6,0.7), 
                   eta = c(0.1,0.2),
                   gamma=0,
                   min_child_weight = c(5:8),
                   subsample = c(0.6,0.7,0.8,0.9)
)

set.seed(0)

x = mtcars[,-1]
y_train = mtcars[,1]

xgb_model8 = train(
x, y_train,  
trControl = xgb_trcontrol,
tuneGrid = xgbGrid,
method = "xgbTree"
)

您可以这样保存您的绘图:

pdf("plots.pdf")
plot(xgb_model8,metric="RMSE")
dev.off()

或者如果你想绘制一个特定的参数,例如eta = 0.2,你也需要修复colsample_bytree,否则参数太多:

library(ggplot2)

ggplot(subset(xgb_model8$results
,eta==0.1 & colsample_bytree==0.6),
aes(x=min_child_weight,y=RMSE,group=factor(subsample),col=factor(subsample))) + 
geom_line() + geom_point() + facet_grid(nrounds~max_depth)