From 6e370b90fd14f1eb5dc24cecb8c2bed93e133e62 Mon Sep 17 00:00:00 2001 From: pommedeterresautee Date: Mon, 30 Nov 2015 15:47:10 +0100 Subject: [PATCH] some fixes for Travis #Rstat --- R-package/R/xgb.model.dt.tree.R | 2 +- R-package/R/xgb.plot.deepness.R | 2 +- R-package/R/xgb.plot.multi.trees.R | 7 ++++--- R-package/R/xgb.plot.tree.R | 17 ++++++----------- R-package/man/xgb.model.dt.tree.Rd | 2 +- R-package/man/xgb.plot.deepness.Rd | 2 +- R-package/man/xgb.plot.multi.trees.Rd | 8 +++++--- R-package/man/xgb.plot.tree.Rd | 8 +++----- R-package/tests/testthat/test_helpers.R | 6 +++--- 9 files changed, 25 insertions(+), 29 deletions(-) diff --git a/R-package/R/xgb.model.dt.tree.R b/R-package/R/xgb.model.dt.tree.R index 29ef2e1df..4d8e10e3b 100644 --- a/R-package/R/xgb.model.dt.tree.R +++ b/R-package/R/xgb.model.dt.tree.R @@ -50,7 +50,7 @@ #' eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") #' #' #agaricus.test$data@@Dimnames[[2]] represents the column names of the sparse matrix. -#' xgb.model.dt.tree(agaricus.train$data@@Dimnames[[2]], model = bst) +#' xgb.model.dt.tree(feature_names = agaricus.train$data@@Dimnames[[2]], model = bst) #' #' @export xgb.model.dt.tree <- function(feature_names = NULL, model = NULL, text = NULL, n_first_tree = NULL){ diff --git a/R-package/R/xgb.plot.deepness.R b/R-package/R/xgb.plot.deepness.R index b6c05f727..bebb7605a 100644 --- a/R-package/R/xgb.plot.deepness.R +++ b/R-package/R/xgb.plot.deepness.R @@ -93,7 +93,7 @@ get.paths.to.leaf <- function(dt.tree) { #' @examples #' data(agaricus.train, package='xgboost') #' -#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max.depth = 15, +#' bst <- xgboost(data = agaricus.train$data, max.depth = 15, #' eta = 1, nthread = 2, nround = 30, objective = "binary:logistic", #' min_child_weight = 50) #' diff --git a/R-package/R/xgb.plot.multi.trees.R b/R-package/R/xgb.plot.multi.trees.R index 1efa375a4..f140a959f 100644 --- a/R-package/R/xgb.plot.multi.trees.R +++ b/R-package/R/xgb.plot.multi.trees.R @@ -11,6 +11,7 @@ #' @importFrom stringr str_extract #' #' @param model dump generated by the \code{xgb.train} function. Avoid the creation of a dump file. +#' @param feature_names names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be \code{NULL}. #' @param features.keep number of features to keep in each position of the multi trees. #' @param plot.width width in pixels of the graph to produce #' @param plot.height height in pixels of the graph to produce @@ -45,12 +46,12 @@ #' eta = 1, nthread = 2, nround = 30, objective = "binary:logistic", #' min_child_weight = 50) #' -#' p <- xgb.plot.multi.trees(model = bst, names = agaricus.train$data@Dimnames[[2]], 3) +#' p <- xgb.plot.multi.trees(model = bst, feature_names = agaricus.train$data@Dimnames[[2]], features.keep = 3) #' print(p) #' #' @export -xgb.plot.multi.trees <- function(model, names, features.keep = 5, plot.width = NULL, plot.height = NULL){ - tree.matrix <- xgb.model.dt.tree(names, model = model) +xgb.plot.multi.trees <- function(model, feature_names = NULL, features.keep = 5, plot.width = NULL, plot.height = NULL){ + tree.matrix <- xgb.model.dt.tree(feature_names = feature_names, model = model) # first number of the path represents the tree, then the following numbers are related to the path to follow # root init diff --git a/R-package/R/xgb.plot.tree.R b/R-package/R/xgb.plot.tree.R index 2976f1b07..ea7fabef7 100644 --- a/R-package/R/xgb.plot.tree.R +++ b/R-package/R/xgb.plot.tree.R @@ -7,7 +7,6 @@ #' @importFrom data.table := #' @importFrom magrittr %>% #' @param feature_names names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be \code{NULL}. -#' @param filename_dump the path to the text file storing the model. Model dump must include the gain per feature and per tree (parameter \code{with.stats = T} in function \code{xgb.dump}). Possible to provide a model directly (see \code{model} argument). #' @param model generated by the \code{xgb.train} function. Avoid the creation of a dump file. #' @param n_first_tree limit the plot to the n first trees. If \code{NULL}, all trees of the model are plotted. Performance can be low for huge models. #' @param plot.width the width of the diagram in pixels. @@ -40,25 +39,21 @@ #' eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") #' #' #agaricus.test$data@@Dimnames[[2]] represents the column names of the sparse matrix. -#' xgb.plot.tree(agaricus.train$data@@Dimnames[[2]], model = bst) +#' xgb.plot.tree(feature_names = agaricus.train$data@@Dimnames[[2]], model = bst) #' #' @export -xgb.plot.tree <- function(feature_names = NULL, filename_dump = NULL, model = NULL, n_first_tree = NULL, plot.width = NULL, plot.height = NULL){ +xgb.plot.tree <- function(feature_names = NULL, model = NULL, n_first_tree = NULL, plot.width = NULL, plot.height = NULL){ - if (!class(model) %in% c("xgb.Booster", "NULL")) { + if (class(model) != "xgb.Booster") { stop("model: Has to be an object of class xgb.Booster model generaged by the xgb.train function.") } if (!requireNamespace("DiagrammeR", quietly = TRUE)) { stop("DiagrammeR package is required for xgb.plot.tree", call. = FALSE) } - - if(is.null(model)){ - allTrees <- xgb.model.dt.tree(feature_names = feature_names, filename_dump = filename_dump, n_first_tree = n_first_tree) - } else { - allTrees <- xgb.model.dt.tree(feature_names = feature_names, model = model, n_first_tree = n_first_tree) - } - + + allTrees <- xgb.model.dt.tree(feature_names = feature_names, model = model, n_first_tree = n_first_tree) + allTrees[, label:= paste0(Feature, "\nCover: ", Cover, "\nGain: ", Quality)] allTrees[, shape:= "rectangle"][Feature == "Leaf", shape:= "oval"] allTrees[, filledcolor:= "Beige"][Feature == "Leaf", filledcolor:= "Khaki"] diff --git a/R-package/man/xgb.model.dt.tree.Rd b/R-package/man/xgb.model.dt.tree.Rd index 8d88f60f5..7dadb20aa 100644 --- a/R-package/man/xgb.model.dt.tree.Rd +++ b/R-package/man/xgb.model.dt.tree.Rd @@ -52,7 +52,7 @@ bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") #agaricus.test$data@Dimnames[[2]] represents the column names of the sparse matrix. -xgb.model.dt.tree(agaricus.train$data@Dimnames[[2]], model = bst) +xgb.model.dt.tree(feature_names = agaricus.train$data@Dimnames[[2]], model = bst) } diff --git a/R-package/man/xgb.plot.deepness.Rd b/R-package/man/xgb.plot.deepness.Rd index 6488514dd..d011a4dc5 100644 --- a/R-package/man/xgb.plot.deepness.Rd +++ b/R-package/man/xgb.plot.deepness.Rd @@ -35,7 +35,7 @@ This function is inspired by this blog post \url{http://aysent.github.io/2015/11 \examples{ data(agaricus.train, package='xgboost') -bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max.depth = 15, +bst <- xgboost(data = agaricus.train$data, max.depth = 15, eta = 1, nthread = 2, nround = 30, objective = "binary:logistic", min_child_weight = 50) diff --git a/R-package/man/xgb.plot.multi.trees.Rd b/R-package/man/xgb.plot.multi.trees.Rd index 6e59915e2..2d0a1d3e8 100644 --- a/R-package/man/xgb.plot.multi.trees.Rd +++ b/R-package/man/xgb.plot.multi.trees.Rd @@ -4,12 +4,14 @@ \alias{xgb.plot.multi.trees} \title{Project all trees on one tree and plot it} \usage{ -xgb.plot.multi.trees(model, names, features.keep = 5, plot.width = NULL, - plot.height = NULL) +xgb.plot.multi.trees(model, feature_names = NULL, features.keep = 5, + plot.width = NULL, plot.height = NULL) } \arguments{ \item{model}{dump generated by the \code{xgb.train} function. Avoid the creation of a dump file.} +\item{feature_names}{names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be \code{NULL}.} + \item{features.keep}{number of features to keep in each position of the multi trees.} \item{plot.width}{width in pixels of the graph to produce} @@ -49,7 +51,7 @@ bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max.dep eta = 1, nthread = 2, nround = 30, objective = "binary:logistic", min_child_weight = 50) -p <- xgb.plot.multi.trees(model = bst, names = agaricus.train$data@Dimnames[[2]], 3) +p <- xgb.plot.multi.trees(model = bst, feature_names = agaricus.train$data@Dimnames[[2]], features.keep = 3) print(p) } diff --git a/R-package/man/xgb.plot.tree.Rd b/R-package/man/xgb.plot.tree.Rd index 2008014cf..16e80f9ee 100644 --- a/R-package/man/xgb.plot.tree.Rd +++ b/R-package/man/xgb.plot.tree.Rd @@ -4,14 +4,12 @@ \alias{xgb.plot.tree} \title{Plot a boosted tree model} \usage{ -xgb.plot.tree(feature_names = NULL, filename_dump = NULL, model = NULL, - n_first_tree = NULL, plot.width = NULL, plot.height = NULL) +xgb.plot.tree(feature_names = NULL, model = NULL, n_first_tree = NULL, + plot.width = NULL, plot.height = NULL) } \arguments{ \item{feature_names}{names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be \code{NULL}.} -\item{filename_dump}{the path to the text file storing the model. Model dump must include the gain per feature and per tree (parameter \code{with.stats = T} in function \code{xgb.dump}). Possible to provide a model directly (see \code{model} argument).} - \item{model}{generated by the \code{xgb.train} function. Avoid the creation of a dump file.} \item{n_first_tree}{limit the plot to the n first trees. If \code{NULL}, all trees of the model are plotted. Performance can be low for huge models.} @@ -51,7 +49,7 @@ bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") #agaricus.test$data@Dimnames[[2]] represents the column names of the sparse matrix. -xgb.plot.tree(agaricus.train$data@Dimnames[[2]], model = bst) +xgb.plot.tree(feature_names = agaricus.train$data@Dimnames[[2]], model = bst) } diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R index 490b6b867..d4e547de5 100644 --- a/R-package/tests/testthat/test_helpers.R +++ b/R-package/tests/testthat/test_helpers.R @@ -23,13 +23,13 @@ test_that("xgb.dump works", { }) test_that("xgb.importance works", { - importance <- xgb.importance(sparse_matrix@Dimnames[[2]], model = bst) + importance <- xgb.importance(feature_names = sparse_matrix@Dimnames[[2]], model = bst) expect_equal(dim(importance), c(7, 4)) expect_equal(colnames(importance), c("Feature", "Gain", "Cover", "Frequency")) }) test_that("xgb.plot.tree works", { - xgb.plot.tree(names = agaricus.train$data@Dimnames[[2]], model = bst) + xgb.plot.tree(feature_names = agaricus.train$data@Dimnames[[2]], model = bst) }) test_that("xgb.plot.deepness works", { @@ -37,5 +37,5 @@ test_that("xgb.plot.deepness works", { }) test_that("xgb.plot.multi.trees works", { - xgb.plot.multi.trees(model = bst, names = agaricus.train$data@Dimnames[[2]], 3) + xgb.plot.multi.trees(model = bst, feature_names = agaricus.train$data@Dimnames[[2]], 3) }) \ No newline at end of file