【发布时间】:2017-04-29 19:53:43
【问题描述】:
我使用以下代码来调整我的 Xgboost 实现的参数,改编自 here:
searchGridSubCol <- expand.grid(subsample = c(0.5, 0.75, 1),
colsample_bytree = c(0.6, 0.8, 1))
ntrees <- 100
#Build a xgb.DMatrix object
#DMMatrixTrain <- xgb.DMatrix(data = yourMatrix, label = yourTarget)
rmseErrorsHyperparameters <- apply(searchGridSubCol, 1, function(parameterList){
#Extract Parameters to test
currentSubsampleRate <- parameterList[["subsample"]]
currentColsampleRate <- parameterList[["colsample_bytree"]]
xgboostModelCV <- xgb.cv(data = as.matrix(train), nrounds = ntrees, nfold = 5, showsd = TRUE, label = traintarget,
metrics = "rmse", verbose = TRUE, "eval_metric" = "rmse",
"objective" = "reg:linear", "max.depth" = 15, "eta" = 2/ntrees,
"subsample" = currentSubsampleRate, "colsample_bytree" = currentColsampleRate)
xvalidationScores <- as.data.frame(xgboostModelCV)
#Save rmse of the last iteration
rmse <- tail(xvalidationScores$test.rmse.mean, 1)
return(c(rmse, currentSubsampleRate, currentColsampleRate))
})
但是,我在存储 XGBoostModelCV 时收到以下错误:
Error in as.data.frame.default(xgboostModelCV) :
cannot coerce class ""xgb.cv.synchronous"" to a data.frame
谁能向我解释导致此错误的原因以及我该如何解决?
【问题讨论】:
-
帮助说 xgb.cv 的返回属于
xgb.cv.synchronous类,所以它很可能不是数据框,建议print(xgboostModelCV, verbose=TRUE)并寻找可能感兴趣的结构。应该有类似xgboostModelCV$evaluation_log或xgboostModelCV$best_iteration -
我发现了问题,在我删除 as.dataframe 时使用的 xgboost 版本中它工作正常
-
不,不固定
-
有没有人如何修复上面的代码?