diff --git a/R-package/NAMESPACE b/R-package/NAMESPACE index 5c9e19932..bd12fc7ec 100644 --- a/R-package/NAMESPACE +++ b/R-package/NAMESPACE @@ -17,9 +17,13 @@ import(methods) importClassesFrom(Matrix,dgCMatrix) importClassesFrom(Matrix,dgeMatrix) importFrom(data.table,":=") +importFrom(data.table,as.data.table) importFrom(data.table,data.table) importFrom(data.table,rbindlist) importFrom(magrittr,"%>%") importFrom(stringr,str_extract) importFrom(stringr,str_extract_all) +importFrom(stringr,str_match) +importFrom(stringr,str_replace) +importFrom(stringr,str_replace_all) importFrom(stringr,str_split) diff --git a/R-package/R/xgb.cv.R b/R-package/R/xgb.cv.R index a5be567bc..c2e73e202 100644 --- a/R-package/R/xgb.cv.R +++ b/R-package/R/xgb.cv.R @@ -3,11 +3,15 @@ #' The cross valudation function of xgboost #' #' @importFrom data.table data.table +#' @importFrom data.table as.data.table #' @importFrom magrittr %>% #' @importFrom data.table := #' @importFrom data.table rbindlist #' @importFrom stringr str_extract_all #' @importFrom stringr str_split +#' @importFrom stringr str_replace_all +#' @importFrom stringr str_replace +#' @importFrom stringr str_match #' #' @param params the list of parameters. Commonly used ones are: #' \itemize{ @@ -98,14 +102,20 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing = cat(paste(ret, "\n", sep="")) } - dt <- data.table(train_rmse_mean=numeric(), train_rmse_std=numeric(), train_auc_mean=numeric(), train_auc_std=numeric(), test_rmse_mean=numeric(), test_rmse_std=numeric(), test_auc_mean=numeric(), test_auc_std=numeric()) + colnames <- str_split(string = history[1], pattern = "\t")[[1]] %>% .[2:length(.)] %>% str_extract(".*:") %>% str_replace(":","") %>% str_replace_all("-", ".") + + colnamesMean <- paste(colnames, "mean") + colnamesStd <- paste(colnames, "std") + colnames <- c() + for(i in 1:length(colnamesMean)) colnames <- c(colnames, colnamesMean[i], colnamesStd[i]) + + type <- rep(x = "numeric", times = length(colnames)) + + dt <- read.table(text = "", colClasses = type, col.names = colnames) %>% as.data.table split = str_split(string = history, pattern = "\t") for(line in split){ dt <- line[2:length(line)] %>% str_extract_all(pattern = "\\d.\\d*") %>% unlist %>% as.list %>% {vec <- .;rbindlist(list(dt, vec), use.names = F, fill = F)} } dt -} - - - +} \ No newline at end of file