[英]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().
我无法理解以下定义函数xgb.cv.bayes()的R代码。 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(): 我无法理解关闭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. 我将不胜感激理解这种R语法,在哪里可以找到更多的例子。
In R the value of the last expression in a function is automatically the return value of this function. 在R中,函数中最后一个表达式的值自动是该函数的返回值。 So the function you presented has exactly two steps:
因此,您呈现的功能分两个步骤:
xgv.cv(...)
and store the result in a variable cv
xgv.cv(...)
的结果并将结果存储在变量cv
Score
and Pred
) whose values are extracted from cv
. Score
和Pred
)创建一个列表,它们的值是从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
. 因此,如果要执行
test <- xgb.cv.bayes(...)
,则可以访问test$Score
和test$Pred
。 Does this answer your question? 这回答了你的问题了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.