3
votes

I use the following code to tune parameters for my Xgboost implementation adapted from 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))

})

However I recieve the following error when storing the XGBoostModelCV:

 Error in as.data.frame.default(xgboostModelCV) : 
  cannot coerce class ""xgb.cv.synchronous"" to a data.frame

Can someone explain to me what is causing this error and how may I fix it?

1
The help says the return of xgb.cv is of class xgb.cv.synchronous so it most likely isn't a dataframe, suggest print(xgboostModelCV, verbose=TRUE) and look for the structure that may interest. There should be something like xgboostModelCV$evaluation_log or xgboostModelCV$best_iterationAndrew Lavers
I figured out the problem, in the version of xgboost I'm using when I remove the as.dataframe it works fineljourney
Nopes, not fixedljourney
Has anyone how to fix above code?Kalenji

1 Answers

1
votes

The above should be fixed by:

xvalidationScores <- xgboostModelCV
#Save rmse of the last iteration
rmse <- tail(xvalidationScores$evaluation_log$test_rmse_mean, 1)