From 0f0c12707ce81fb099e551403938018ea2cae390 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Aug 2014 15:03:24 -0700 Subject: [PATCH] modify xgb.getinfo to getinfo --- R-package/DESCRIPTION | 4 +- R-package/NAMESPACE | 2 +- R-package/R/getinfo.xgb.DMatrix.R | 21 ++++ R-package/R/predict.xgb.Booster.R | 21 ++-- R-package/R/utils.R | 193 ++++++++++++++++-------------- R-package/R/xgb.DMatrix.R | 44 +++---- R-package/R/xgb.DMatrix.save.R | 23 ++-- R-package/R/xgb.dump.R | 18 +-- R-package/R/xgb.getinfo.R | 16 --- R-package/R/xgb.load.R | 8 +- R-package/R/xgb.save.R | 22 ++-- R-package/R/xgb.train.R | 77 ++++++------ R-package/R/xgboost.R | 67 +++++------ R-package/inst/examples/demo.R | 8 +- 14 files changed, 268 insertions(+), 256 deletions(-) create mode 100644 R-package/R/getinfo.xgb.DMatrix.R delete mode 100644 R-package/R/xgb.getinfo.R diff --git a/R-package/DESCRIPTION b/R-package/DESCRIPTION index a422d6e91..b03af29eb 100644 --- a/R-package/DESCRIPTION +++ b/R-package/DESCRIPTION @@ -3,8 +3,8 @@ Type: Package Title: R wrapper of xgboost Version: 0.3-0 Date: 2014-08-23 -Author: Tianqi Chen -Maintainer: Tianqi Chen +Author: Tianqi Chen, Tong He +Maintainer: Tianqi Chen , Tong He Description: xgboost License: See LICENSE file URL: https://github.com/tqchen/xgboost diff --git a/R-package/NAMESPACE b/R-package/NAMESPACE index 21b6c9ace..f8b409724 100644 --- a/R-package/NAMESPACE +++ b/R-package/NAMESPACE @@ -2,8 +2,8 @@ importClassesFrom("Matrix", dgCMatrix, dgeMatrix) export(xgboost) export(xgb.DMatrix) -export(xgb.getinfo) exportMethods(predict) +exportMethods(getinfo) export(xgb.train) export(xgb.save) export(xgb.load) diff --git a/R-package/R/getinfo.xgb.DMatrix.R b/R-package/R/getinfo.xgb.DMatrix.R new file mode 100644 index 000000000..52eddd386 --- /dev/null +++ b/R-package/R/getinfo.xgb.DMatrix.R @@ -0,0 +1,21 @@ +setClass('xgb.DMatrix') + +getinfo <- function(object, ...){ + UseMethod("getinfo") +} + +setMethod("getinfo", signature = "xgb.DMatrix", + definition = function(object, name) { + if (typeof(name) != "character") { + stop("xgb.getinfo: name must be character") + } + if (class(object) != "xgb.DMatrix") { + stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix") + } + if (name != "label" && name != "weight" && name != "base_margin") { + stop(paste("xgb.getinfo: unknown info name", name)) + } + ret <- .Call("XGDMatrixGetInfo_R", object, name, PACKAGE = "xgboost") + return(ret) + }) + diff --git a/R-package/R/predict.xgb.Booster.R b/R-package/R/predict.xgb.Booster.R index 25cb5d6aa..4fd3688fb 100644 --- a/R-package/R/predict.xgb.Booster.R +++ b/R-package/R/predict.xgb.Booster.R @@ -2,15 +2,12 @@ setClass("xgb.Booster") #' @export -setMethod("predict", - signature = "xgb.Booster", - definition = function(object, newdata, outputmargin = FALSE) - { - if (class(newdata) != "xgb.DMatrix") { - newdata = xgb.DMatrix(newdata) - } - ret <- .Call("XGBoosterPredict_R", object, newdata, - as.integer(outputmargin), PACKAGE="xgboost") - return(ret) - }) - +setMethod("predict", signature = "xgb.Booster", + definition = function(object, newdata, outputmargin = FALSE) { + if (class(newdata) != "xgb.DMatrix") { + newdata <- xgb.DMatrix(newdata) + } + ret <- .Call("XGBoosterPredict_R", object, newdata, as.integer(outputmargin), PACKAGE = "xgboost") + return(ret) +}) + diff --git a/R-package/R/utils.R b/R-package/R/utils.R index 5ed4c8979..afb012211 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -1,128 +1,135 @@ # depends on matrix .onLoad <- function(libname, pkgname) { - library.dynam("xgboost", pkgname, libname); + library.dynam("xgboost", pkgname, libname) } .onUnload <- function(libpath) { - library.dynam.unload("xgboost", libpath); + library.dynam.unload("xgboost", libpath) } # set information into dmatrix, this mutate dmatrix xgb.setinfo <- function(dmat, name, info) { - if (class(dmat) != "xgb.DMatrix") { - stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix"); - } - if (name == "label") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), PACKAGE="xgboost") - return(TRUE) - } - if (name == "weight") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), PACKAGE="xgboost") - return(TRUE) - } - if (name == "base_margin") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), PACKAGE="xgboost") - return(TRUE) - } - if (name == "group") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.integer(info), PACKAGE="xgboost") - return(TRUE) - } - stop(paste("xgb.setinfo: unknown info name", name)) - return(FALSE) + if (class(dmat) != "xgb.DMatrix") { + stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix") + } + if (name == "label") { + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), + PACKAGE = "xgboost") + return(TRUE) + } + if (name == "weight") { + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), + PACKAGE = "xgboost") + return(TRUE) + } + if (name == "base_margin") { + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), + PACKAGE = "xgboost") + return(TRUE) + } + if (name == "group") { + .Call("XGDMatrixSetInfo_R", dmat, name, as.integer(info), + PACKAGE = "xgboost") + return(TRUE) + } + stop(paste("xgb.setinfo: unknown info name", name)) + return(FALSE) } # construct a Booster from cachelist xgb.Booster <- function(params = list(), cachelist = list(), modelfile = NULL) { - if (typeof(cachelist) != "list") { - stop("xgb.Booster: only accepts list of DMatrix as cachelist") + if (typeof(cachelist) != "list") { + stop("xgb.Booster: only accepts list of DMatrix as cachelist") + } + for (dm in cachelist) { + if (class(dm) != "xgb.DMatrix") { + stop("xgb.Booster: only accepts list of DMatrix as cachelist") } - for (dm in cachelist) { - if (class(dm) != "xgb.DMatrix") { - stop("xgb.Booster: only accepts list of DMatrix as cachelist") - } + } + handle <- .Call("XGBoosterCreate_R", cachelist, PACKAGE = "xgboost") + .Call("XGBoosterSetParam_R", handle, "seed", "0", PACKAGE = "xgboost") + if (length(params) != 0) { + for (i in 1:length(params)) { + p <- params[i] + .Call("XGBoosterSetParam_R", handle, names(p), as.character(p), + PACKAGE = "xgboost") } - handle <- .Call("XGBoosterCreate_R", cachelist, PACKAGE="xgboost") - .Call("XGBoosterSetParam_R", handle, "seed", "0", PACKAGE="xgboost") - if (length(params) != 0) { - for (i in 1:length(params)) { - p <- params[i] - .Call("XGBoosterSetParam_R", handle, names(p), as.character(p), PACKAGE="xgboost") - } + } + if (!is.null(modelfile)) { + if (typeof(modelfile) != "character") { + stop("xgb.Booster: modelfile must be character") } - if (!is.null(modelfile)) { - if (typeof(modelfile) != "character"){ - stop("xgb.Booster: modelfile must be character"); - } - .Call("XGBoosterLoadModel_R", handle, modelfile, PACKAGE="xgboost") - } - return(structure(handle, class="xgb.Booster")) + .Call("XGBoosterLoadModel_R", handle, modelfile, PACKAGE = "xgboost") + } + return(structure(handle, class = "xgb.Booster")) } # predict, depreciated xgb.predict <- function(booster, dmat, outputmargin = FALSE) { - if (class(booster) != "xgb.Booster") { - stop("xgb.predict: first argument must be type xgb.Booster") - } - if (class(dmat) != "xgb.DMatrix") { - stop("xgb.predict: second argument must be type xgb.DMatrix") - } - ret <- .Call("XGBoosterPredict_R", booster, dmat, as.integer(outputmargin), PACKAGE="xgboost") - return(ret) + if (class(booster) != "xgb.Booster") { + stop("xgb.predict: first argument must be type xgb.Booster") + } + if (class(dmat) != "xgb.DMatrix") { + stop("xgb.predict: second argument must be type xgb.DMatrix") + } + ret <- .Call("XGBoosterPredict_R", booster, dmat, as.integer(outputmargin), + PACKAGE = "xgboost") + return(ret) } -##-------------------------------------- -# the following are low level iteratively function, not needed -# if you do not want to use them -#--------------------------------------- +## ----the following are low level iteratively function, not needed if +## you do not want to use them --------------------------------------- # iteratively update booster with dtrain xgb.iter.update <- function(booster, dtrain, iter) { - if (class(booster) != "xgb.Booster") { - stop("xgb.iter.update: first argument must be type xgb.Booster") - } - if (class(dtrain) != "xgb.DMatrix") { - stop("xgb.iter.update: second argument must be type xgb.DMatrix") - } - .Call("XGBoosterUpdateOneIter_R", booster, as.integer(iter), dtrain, PACKAGE="xgboost") - return(TRUE) + if (class(booster) != "xgb.Booster") { + stop("xgb.iter.update: first argument must be type xgb.Booster") + } + if (class(dtrain) != "xgb.DMatrix") { + stop("xgb.iter.update: second argument must be type xgb.DMatrix") + } + .Call("XGBoosterUpdateOneIter_R", booster, as.integer(iter), dtrain, + PACKAGE = "xgboost") + return(TRUE) } # iteratively update booster with customized statistics xgb.iter.boost <- function(booster, dtrain, gpair) { - if (class(booster) != "xgb.Booster") { - stop("xgb.iter.update: first argument must be type xgb.Booster") - } - if (class(dtrain) != "xgb.DMatrix") { - stop("xgb.iter.update: second argument must be type xgb.DMatrix") - } - .Call("XGBoosterBoostOneIter_R", booster, dtrain, gpair$grad, gpair$hess, PACKAGE="xgboost") - return(TRUE) + if (class(booster) != "xgb.Booster") { + stop("xgb.iter.update: first argument must be type xgb.Booster") + } + if (class(dtrain) != "xgb.DMatrix") { + stop("xgb.iter.update: second argument must be type xgb.DMatrix") + } + .Call("XGBoosterBoostOneIter_R", booster, dtrain, gpair$grad, gpair$hess, + PACKAGE = "xgboost") + return(TRUE) } # iteratively evaluate one iteration xgb.iter.eval <- function(booster, watchlist, iter) { - if (class(booster) != "xgb.Booster") { - stop("xgb.eval: first argument must be type xgb.Booster") + if (class(booster) != "xgb.Booster") { + stop("xgb.eval: first argument must be type xgb.Booster") + } + if (typeof(watchlist) != "list") { + stop("xgb.eval: only accepts list of DMatrix as watchlist") + } + for (w in watchlist) { + if (class(w) != "xgb.DMatrix") { + stop("xgb.eval: watch list can only contain xgb.DMatrix") } - if (typeof(watchlist) != "list") { - stop("xgb.eval: only accepts list of DMatrix as watchlist") + } + evnames <- list() + if (length(watchlist) != 0) { + for (i in 1:length(watchlist)) { + w <- watchlist[i] + if (length(names(w)) == 0) { + stop("xgb.eval: name tag must be presented for every elements in watchlist") + } + evnames <- append(evnames, names(w)) } - for (w in watchlist) { - if (class(w) != "xgb.DMatrix") { - stop("xgb.eval: watch list can only contain xgb.DMatrix") - } - } - evnames <- list() - if (length(watchlist) != 0) { - for (i in 1:length(watchlist)) { - w <- watchlist[i] - if (length(names(w)) == 0) { - stop("xgb.eval: name tag must be presented for every elements in watchlist") - } - evnames <- append(evnames, names(w)) - } - } - msg <- .Call("XGBoosterEvalOneIter_R", booster, as.integer(iter), watchlist, evnames, PACKAGE="xgboost") - return(msg) -} + } + msg <- .Call("XGBoosterEvalOneIter_R", booster, as.integer(iter), watchlist, + evnames, PACKAGE = "xgboost") + return(msg) +} diff --git a/R-package/R/xgb.DMatrix.R b/R-package/R/xgb.DMatrix.R index e076de18f..37b1b3060 100644 --- a/R-package/R/xgb.DMatrix.R +++ b/R-package/R/xgb.DMatrix.R @@ -1,22 +1,26 @@ # constructing DMatrix -xgb.DMatrix <- function(data, info=list(), missing=0.0, ...) { - if (typeof(data) == "character") { - handle <- .Call("XGDMatrixCreateFromFile_R", data, as.integer(FALSE), PACKAGE="xgboost") - } else if(is.matrix(data)) { - handle <- .Call("XGDMatrixCreateFromMat_R", data, missing, PACKAGE="xgboost") - } else if(class(data) == "dgCMatrix") { - handle <- .Call("XGDMatrixCreateFromCSC_R", data@p, data@i, data@x, PACKAGE="xgboost") - } else { - stop(paste("xgb.DMatrix: does not support to construct from ", typeof(data))) - } - dmat <- structure(handle, class="xgb.DMatrix") - - info = append(info,list(...)) - if (length(info)==0) - return(dmat) - for (i in 1:length(info)) { - p = info[i] - xgb.setinfo(dmat, names(p), p[[1]]) - } +xgb.DMatrix <- function(data, info = list(), missing = 0, ...) { + if (typeof(data) == "character") { + handle <- .Call("XGDMatrixCreateFromFile_R", data, as.integer(FALSE), + PACKAGE = "xgboost") + } else if (is.matrix(data)) { + handle <- .Call("XGDMatrixCreateFromMat_R", data, missing, + PACKAGE = "xgboost") + } else if (class(data) == "dgCMatrix") { + handle <- .Call("XGDMatrixCreateFromCSC_R", data@p, data@i, data@x, + PACKAGE = "xgboost") + } else { + stop(paste("xgb.DMatrix: does not support to construct from ", + typeof(data))) + } + dmat <- structure(handle, class = "xgb.DMatrix") + + info <- append(info, list(...)) + if (length(info) == 0) return(dmat) -} + for (i in 1:length(info)) { + p <- info[i] + xgb.setinfo(dmat, names(p), p[[1]]) + } + return(dmat) +} diff --git a/R-package/R/xgb.DMatrix.save.R b/R-package/R/xgb.DMatrix.save.R index 98400e2f2..6b6d6e136 100644 --- a/R-package/R/xgb.DMatrix.save.R +++ b/R-package/R/xgb.DMatrix.save.R @@ -1,12 +1,13 @@ -# save model or DMatrix to file +# save model or DMatrix to file xgb.DMatrix.save <- function(handle, fname) { - if (typeof(fname) != "character") { - stop("xgb.save: fname must be character") - } - if (class(handle) == "xgb.DMatrix") { - .Call("XGDMatrixSaveBinary_R", handle, fname, as.integer(FALSE), PACKAGE="xgboost") - return(TRUE) - } - stop("xgb.save: the input must be either xgb.DMatrix or xgb.Booster") - return(FALSE) -} + if (typeof(fname) != "character") { + stop("xgb.save: fname must be character") + } + if (class(handle) == "xgb.DMatrix") { + .Call("XGDMatrixSaveBinary_R", handle, fname, as.integer(FALSE), + PACKAGE = "xgboost") + return(TRUE) + } + stop("xgb.save: the input must be either xgb.DMatrix or xgb.Booster") + return(FALSE) +} diff --git a/R-package/R/xgb.dump.R b/R-package/R/xgb.dump.R index b53cd8b46..55107499d 100644 --- a/R-package/R/xgb.dump.R +++ b/R-package/R/xgb.dump.R @@ -1,11 +1,11 @@ # dump model xgb.dump <- function(booster, fname, fmap = "") { - if (class(booster) != "xgb.Booster") { - stop("xgb.dump: first argument must be type xgb.Booster") - } - if (typeof(fname) != "character"){ - stop("xgb.dump: second argument must be type character") - } - .Call("XGBoosterDumpModel_R", booster, fname, fmap, PACKAGE="xgboost") - return(TRUE) -} + if (class(booster) != "xgb.Booster") { + stop("xgb.dump: first argument must be type xgb.Booster") + } + if (typeof(fname) != "character") { + stop("xgb.dump: second argument must be type character") + } + .Call("XGBoosterDumpModel_R", booster, fname, fmap, PACKAGE = "xgboost") + return(TRUE) +} diff --git a/R-package/R/xgb.getinfo.R b/R-package/R/xgb.getinfo.R deleted file mode 100644 index a8952826d..000000000 --- a/R-package/R/xgb.getinfo.R +++ /dev/null @@ -1,16 +0,0 @@ -# get information from dmatrix -xgb.getinfo <- function(dmat, name) { - if (typeof(name) != "character") { - stop("xgb.getinfo: name must be character") - } - if (class(dmat) != "xgb.DMatrix") { - stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix"); - } - if (name != "label" && - name != "weight" && - name != "base_margin" ) { - stop(paste("xgb.getinfo: unknown info name", name)) - } - ret <- .Call("XGDMatrixGetInfo_R", dmat, name, PACKAGE="xgboost") - return(ret) -} diff --git a/R-package/R/xgb.load.R b/R-package/R/xgb.load.R index 2d89620ff..bfe7b92d8 100644 --- a/R-package/R/xgb.load.R +++ b/R-package/R/xgb.load.R @@ -1,5 +1,5 @@ xgb.load <- function(modelfile) { - if (is.null(modelfile)) - stop('xgb.load: modelfile cannot be NULL') - xgb.Booster(modelfile=modelfile) -} + if (is.null(modelfile)) + stop("xgb.load: modelfile cannot be NULL") + xgb.Booster(modelfile = modelfile) +} diff --git a/R-package/R/xgb.save.R b/R-package/R/xgb.save.R index c4269a250..44a2cb9e3 100644 --- a/R-package/R/xgb.save.R +++ b/R-package/R/xgb.save.R @@ -1,12 +1,12 @@ -# save model or DMatrix to file +# save model or DMatrix to file xgb.save <- function(handle, fname) { - if (typeof(fname) != "character") { - stop("xgb.save: fname must be character") - } - if (class(handle) == "xgb.Booster") { - .Call("XGBoosterSaveModel_R", handle, fname, PACKAGE="xgboost") - return(TRUE) - } - stop("xgb.save: the input must be either xgb.DMatrix or xgb.Booster") - return(FALSE) -} + if (typeof(fname) != "character") { + stop("xgb.save: fname must be character") + } + if (class(handle) == "xgb.Booster") { + .Call("XGBoosterSaveModel_R", handle, fname, PACKAGE = "xgboost") + return(TRUE) + } + stop("xgb.save: the input must be either xgb.DMatrix or xgb.Booster") + return(FALSE) +} diff --git a/R-package/R/xgb.train.R b/R-package/R/xgb.train.R index ba595f801..19ffc6529 100644 --- a/R-package/R/xgb.train.R +++ b/R-package/R/xgb.train.R @@ -1,38 +1,47 @@ # train a model using given parameters -xgb.train <- function(params, dtrain, nrounds=10, watchlist=list(), obj=NULL, feval=NULL) { - if (typeof(params) != "list") { - stop("xgb.train: first argument params must be list"); +xgb.train <- function(params=list(), dtrain, nrounds = 10, watchlist = list(), + obj = NULL, feval = NULL, ...) { + if (typeof(params) != "list") { + stop("xgb.train: first argument params must be list") + } + if (class(dtrain) != "xgb.DMatrix") { + stop("xgb.train: second argument dtrain must be xgb.DMatrix") + } + params = append(params, list(...)) + bst <- xgb.Booster(params, append(watchlist, dtrain)) + for (i in 1:nrounds) { + if (is.null(obj)) { + succ <- xgb.iter.update(bst, dtrain, i - 1) + } else { + pred <- xgb.predict(bst, dtrain) + gpair <- obj(pred, dtrain) + succ <- xgb.iter.boost(bst, dtrain, gpair) } - if (class(dtrain) != "xgb.DMatrix") { - stop("xgb.train: second argument dtrain must be xgb.DMatrix"); - } - bst <- xgb.Booster(params, append(watchlist,dtrain)) - for (i in 1:nrounds) { - if (is.null(obj)) { - succ <- xgb.iter.update(bst, dtrain, i-1) - } else { - pred <- xgb.predict(bst, dtrain) - gpair <- obj(pred, dtrain) - succ <- xgb.iter.boost(bst, dtrain, gpair) - } - if (length(watchlist) != 0) { - if (is.null(feval)) { - msg <- xgb.iter.eval(bst, watchlist, i-1) - cat(msg); cat("\n") - } else { - cat("["); cat(i); cat("]"); - for (j in 1:length(watchlist)) { - w <- watchlist[j] - if (length(names(w)) == 0) { - stop("xgb.eval: name tag must be presented for every elements in watchlist") - } - ret <- feval(xgb.predict(bst, w[[1]]), w[[1]]) - cat("\t"); cat(names(w)); cat("-"); cat(ret$metric); - cat(":"); cat(ret$value) - } - cat("\n") - } + if (length(watchlist) != 0) { + if (is.null(feval)) { + msg <- xgb.iter.eval(bst, watchlist, i - 1) + cat(msg) + cat("\n") + } else { + cat("[") + cat(i) + cat("]") + for (j in 1:length(watchlist)) { + w <- watchlist[j] + if (length(names(w)) == 0) { + stop("xgb.eval: name tag must be presented for every elements in watchlist") + } + ret <- feval(xgb.predict(bst, w[[1]]), w[[1]]) + cat("\t") + cat(names(w)) + cat("-") + cat(ret$metric) + cat(":") + cat(ret$value) } + cat("\n") + } } - return(bst) -} + } + return(bst) +} diff --git a/R-package/R/xgboost.R b/R-package/R/xgboost.R index c5299e941..c6a232677 100644 --- a/R-package/R/xgboost.R +++ b/R-package/R/xgboost.R @@ -1,41 +1,30 @@ # Main function for xgboost-package -xgboost = function(data=NULL, label = NULL, params=list(), nrounds=10, - verbose = 1, ...) -{ - inClass = class(data) - if (inClass=='dgCMatrix' || inClass=='matrix') - { - if (is.null(label)) - stop('xgboost: need label when data is a matrix') - dtrain = xgb.DMatrix(data, label=y) - } - else - { - if (!is.null(label)) - warning('xgboost: label will be ignored.') - if (inClass=='character') - dtrain = xgb.DMatrix(data) - else if (inClass=='xgb.DMatrix') - dtrain = data - else - stop('xgboost: Invalid input of data') - } - - if (verbose>1) - silent = 0 - else - silent = 1 - - params = append(params, list(silent=silent)) - params = append(params, list(...)) - - if (verbose>0) - watchlist = list(train=dtrain) - else - watchlist = list() - - bst <- xgb.train(params, dtrain, nrounds, watchlist) - - return(bst) -} +xgboost <- function(data = NULL, label = NULL, params = list(), nrounds = 10, + verbose = 1, ...) { + inClass <- class(data) + if (inClass == "dgCMatrix" || inClass == "matrix") { + if (is.null(label)) + stop("xgboost: need label when data is a matrix") + dtrain <- xgb.DMatrix(data, label = y) + } else { + if (!is.null(label)) + warning("xgboost: label will be ignored.") + if (inClass == "character") + dtrain <- xgb.DMatrix(data) else if (inClass == "xgb.DMatrix") + dtrain <- data else stop("xgboost: Invalid input of data") + } + + if (verbose > 1) + silent <- 0 else silent <- 1 + + params <- append(params, list(silent = silent)) + params <- append(params, list(...)) + + if (verbose > 0) + watchlist <- list(train = dtrain) else watchlist <- list() + + bst <- xgb.train(params, dtrain, nrounds, watchlist) + + return(bst) +} diff --git a/R-package/inst/examples/demo.R b/R-package/inst/examples/demo.R index c84bddd63..dc1e29cb3 100644 --- a/R-package/inst/examples/demo.R +++ b/R-package/inst/examples/demo.R @@ -85,8 +85,8 @@ test.y <- csc$label test.x <- csc$data pred <- predict(bst, test.x) -# Extrac label with xgb.getinfo -labels <- xgb.getinfo(dtest, "label") +# Extrac label with getinfo +labels <- getinfo(dtest, "label") err <- as.numeric(sum(as.integer(pred > 0.5) != labels))/length(labels) print(paste("error=", err)) @@ -126,7 +126,7 @@ param <- list(max_depth = 2, eta = 1, silent = 1) # user define objective function, given prediction, return gradient and second order gradient this is # loglikelihood loss logregobj <- function(preds, dtrain) { - labels <- xgb.getinfo(dtrain, "label") + labels <- getinfo(dtrain, "label") preds <- 1/(1 + exp(-preds)) grad <- preds - labels hess <- preds * (1 - preds) @@ -139,7 +139,7 @@ logregobj <- function(preds, dtrain) { # transformation Take this in mind when you use the customization, and maybe you need write customized # evaluation function evalerror <- function(preds, dtrain) { - labels <- xgb.getinfo(dtrain, "label") + labels <- getinfo(dtrain, "label") err <- as.numeric(sum(labels != (preds > 0)))/length(labels) return(list(metric = "error", value = err)) }