我使用R中的Caret包,用R中的'xgbTree'方法训练一个模型。
绘制训练后的模型,如下图所示:调整参数即'eta'=0.2不是我想要的,因为在训练模型之前,我也在expand.grid中定义了eta=0.1作为调整参数,这是最好的调整。所以我想把图中的eta=0.2改为图中函数中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)