This commit is contained in:
tqchen 2014-09-05 22:46:09 -07:00
parent 831a102d48
commit ab238ff831
3 changed files with 16 additions and 6 deletions

View File

@ -208,7 +208,7 @@ xgb.cv.mknfold <- function(dall, nfold, param) {
return (ret) return (ret)
} }
xgb.cv.aggcv <- function(res, showsd = TRUE) { xgb.cv.aggcv <- function(res, showsd = TRUE) {
header = res[[1]] header <- res[[1]]
ret <- header[1] ret <- header[1]
for (i in 2:length(header)) { for (i in 2:length(header)) {
kv <- strsplit(header[i], ":")[[1]] kv <- strsplit(header[i], ":")[[1]]

View File

@ -18,6 +18,9 @@
#' further details. See also inst/examples/demo.R for walkthrough example in R. #' further details. See also inst/examples/demo.R for walkthrough example in R.
#' @param data takes an \code{xgb.DMatrix} as the input. #' @param data takes an \code{xgb.DMatrix} as the input.
#' @param nrounds the max number of iterations #' @param nrounds the max number of iterations
#' @param nfold number of folds used
#' @param label option field, when data is Matrix
#' @param showd boolean, whether show standard deviation of cross validation
#' @param metrics, list of evaluation metrics to be used in corss validation, #' @param metrics, list of evaluation metrics to be used in corss validation,
#' when it is not specified, the evaluation metric is chosen according to objective function. #' when it is not specified, the evaluation metric is chosen according to objective function.
#' Possible options are: #' Possible options are:
@ -28,7 +31,6 @@
#' \item \code{auc} Area under curve #' \item \code{auc} Area under curve
#' \item \code{merror} Exact matching error, used to evaluate multi-class classification #' \item \code{merror} Exact matching error, used to evaluate multi-class classification
#' } #' }
#'
#' @param obj customized objective function. Returns gradient and second order #' @param obj customized objective function. Returns gradient and second order
#' gradient with given prediction and dtrain, #' gradient with given prediction and dtrain,
#' @param feval custimized evaluation function. Returns #' @param feval custimized evaluation function. Returns
@ -47,13 +49,20 @@
#' @export #' @export
#' #'
xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL,
showsd = TRUE, obj = NULL, feval = NULL, ...) { showsd = TRUE, metrics=list(), obj = NULL, feval = NULL, ...) {
if (typeof(params) != "list") { if (typeof(params) != "list") {
stop("xgb.cv: first argument params must be list") stop("xgb.cv: first argument params must be list")
} }
if (nfold <= 1) {
stop("nfold must be bigger than 1")
}
dtrain <- xgb.get.DMatrix(data, label) dtrain <- xgb.get.DMatrix(data, label)
params <- append(params, list(...)) params <- append(params, list(...))
params <- append(params, list(silent=1)) params <- append(params, list(silent=1))
for (mc in metrics) {
params <- append(params, list("eval_metric"=mc))
}
folds <- xgb.cv.mknfold(dtrain, nfold, params) folds <- xgb.cv.mknfold(dtrain, nfold, params)
history <- list() history <- list()
for (i in 1:nrounds) { for (i in 1:nrounds) {

View File

@ -3,7 +3,8 @@ require(methods)
# Directly read in local file # Directly read in local file
dtrain <- xgb.DMatrix("agaricus.txt.train") dtrain <- xgb.DMatrix("agaricus.txt.train")
history <- xgb.cv(list("max_depth"=3, "eta"=1, history <- xgb.cv( data = dtrain, nround=3, nfold = 5, metrics=list("rmse","auc"),
"objective"="binary:logistic"), "max_depth"=3, "eta"=1,
dtrain, nround=3, nfold = 5, "eval_metric"="error") "objective"="binary:logistic")