From 632fdbbf5c9383ea7eecc520e15c64e6c12368a5 Mon Sep 17 00:00:00 2001 From: tqchen Date: Mon, 19 Jan 2015 09:07:37 -0800 Subject: [PATCH] add proptype of predleaf in R, fix bug in lambda rank --- R-package/R/predict.xgb.Booster.R | 12 ++++++++++-- R-package/src/xgboost_R.cpp | 4 ++-- R-package/src/xgboost_R.h | 4 ++-- src/learner/objective-inl.hpp | 4 ++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/R-package/R/predict.xgb.Booster.R b/R-package/R/predict.xgb.Booster.R index d57017b65..b41f2428c 100644 --- a/R-package/R/predict.xgb.Booster.R +++ b/R-package/R/predict.xgb.Booster.R @@ -11,6 +11,7 @@ setClass("xgb.Booster") #' value of sum of functions, when outputmargin=TRUE, the prediction is #' untransformed margin value. In logistic regression, outputmargin=T will #' output value before logistic transformation. +#' @param predleaf whether predict leaf index instead #' @param ntreelimit limit number of trees used in prediction, this parameter is #' only valid for gbtree, but not for gblinear. set it to be value bigger #' than 0. It will use all trees by default. @@ -25,7 +26,7 @@ setClass("xgb.Booster") #' @export #' setMethod("predict", signature = "xgb.Booster", - definition = function(object, newdata, missing = NULL, outputmargin = FALSE, ntreelimit = NULL) { + definition = function(object, newdata, missing = NULL, outputmargin = FALSE, ntreelimit = NULL, predleaf = FALSE) { if (class(newdata) != "xgb.DMatrix") { if (is.null(missing)) { newdata <- xgb.DMatrix(newdata) @@ -40,7 +41,14 @@ setMethod("predict", signature = "xgb.Booster", stop("predict: ntreelimit must be equal to or greater than 1") } } - ret <- .Call("XGBoosterPredict_R", object, newdata, as.integer(outputmargin), as.integer(ntreelimit), PACKAGE = "xgboost") + option = 0 + if (outputmargin) { + option <- option + 1 + } + if (predleaf) { + option <- option + 2 + } + ret <- .Call("XGBoosterPredict_R", object, newdata, as.integer(predleaf), as.integer(ntreelimit), PACKAGE = "xgboost") return(ret) }) diff --git a/R-package/src/xgboost_R.cpp b/R-package/src/xgboost_R.cpp index b837d45fc..b4757542d 100644 --- a/R-package/src/xgboost_R.cpp +++ b/R-package/src/xgboost_R.cpp @@ -248,12 +248,12 @@ extern "C" { asInteger(iter), BeginPtr(vec_dmats), BeginPtr(vec_sptr), len)); } - SEXP XGBoosterPredict_R(SEXP handle, SEXP dmat, SEXP output_margin, SEXP ntree_limit) { + SEXP XGBoosterPredict_R(SEXP handle, SEXP dmat, SEXP option_mask, SEXP ntree_limit) { _WrapperBegin(); bst_ulong olen; const float *res = XGBoosterPredict(R_ExternalPtrAddr(handle), R_ExternalPtrAddr(dmat), - asInteger(output_margin), + asInteger(option_mask), asInteger(ntree_limit), &olen); SEXP ret = PROTECT(allocVector(REALSXP, olen)); diff --git a/R-package/src/xgboost_R.h b/R-package/src/xgboost_R.h index 766152699..1e7606dd7 100644 --- a/R-package/src/xgboost_R.h +++ b/R-package/src/xgboost_R.h @@ -111,10 +111,10 @@ extern "C" { * \brief make prediction based on dmat * \param handle handle * \param dmat data matrix - * \param output_margin whether only output raw margin value + * \param option_mask output_margin:1 predict_leaf:2 * \param ntree_limit limit number of trees used in prediction */ - SEXP XGBoosterPredict_R(SEXP handle, SEXP dmat, SEXP output_margin, SEXP ntree_limit); + SEXP XGBoosterPredict_R(SEXP handle, SEXP dmat, SEXP option_mask, SEXP ntree_limit); /*! * \brief load model from existing file * \param handle handle diff --git a/src/learner/objective-inl.hpp b/src/learner/objective-inl.hpp index 69b7ae4fd..7702774f9 100644 --- a/src/learner/objective-inl.hpp +++ b/src/learner/objective-inl.hpp @@ -348,9 +348,9 @@ class LambdaRankObj : public IObjFunction { float h = loss.SecondOrderGradient(p, 1.0f); // accumulate gradient and hessian in both pid, and nid gpair[pos.rindex].grad += g * w; - gpair[pos.rindex].hess += 2.0f * h; + gpair[pos.rindex].hess += 2.0f * w * h; gpair[neg.rindex].grad -= g * w; - gpair[neg.rindex].hess += 2.0f * h; + gpair[neg.rindex].hess += 2.0f * w * h; } } }