简体   繁体   中英

Understanding R-syntax in the code for Bayesian Optimization

This is with reference to this answer on implementation of Bayesian Optimization. I am unable to understand the following R-code that defines a function xgb.cv.bayes(). The code is as follows:

xgb.cv.bayes <- function(max.depth, min_child_weight, subsample, colsample_bytree, gamma){
  cv <- xgv.cv(params = list(booster = 'gbtree', eta = 0.05,
                         max_depth = max.depth,
                         min_child_weight = min_child_weight,
                         subsample = subsample,
                         colsample_bytree = colsample_bytree,
                         gamma = gamma,
                         lambda = 1, alpha = 0,
                         objective = 'binary:logistic',
                         eval_metric = 'auc'),
             data = data.matrix(df.train[,-target.var]),
             label = as.matrix(df.train[, target.var]),
             nround = 500, folds = cv_folds, prediction = TRUE,
             showsd = TRUE, early.stop.round = 5, maximize = TRUE,
             verbose = 0
  )
  list(Score = cv$dt[, max(test.auc.mean)],
   Pred = cv$pred)
}

I am unable to understand the following part of code that comes after closing parenthesis of xgb.cv():

list(Score = cv$dt[, max(test.auc.mean)],
   Pred = cv$pred)

Or very briefly, I do not understand the following syntax:

xgb.cv.bayes <- function(max.depth, min_child_weight, subsample, colsample_bytree, gamma){
  cv <- xgv.cv(...)list(...)
}

I will be grateful in understanding this R-syntax and where can I find more examples of this.

In R the value of the last expression in a function is automatically the return value of this function. So the function you presented has exactly two steps:

  1. compute the result of xgv.cv(...) and store the result in a variable cv
  2. create a list with two entries ( Score and Pred ) whose values are extracted from cv .

Since the expression that creates the list is the last expression in the function, the list is automatically the return value. So, if you would execute test <- xgb.cv.bayes(...) you could then access test$Score and test$Pred . Does this answer your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM