From 78afd6c7729929f1bea0ed7e902f80fe939d8c1c Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 21:36:52 -0400 Subject: [PATCH 1/8] TST: Added test for dump --- R-package/tests/testthat/test_helpers.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 R-package/tests/testthat/test_helpers.R diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R new file mode 100644 index 000000000..a7656f072 --- /dev/null +++ b/R-package/tests/testthat/test_helpers.R @@ -0,0 +1,17 @@ +context('Test helper functions') + +require(xgboost) + +data(agaricus.train, package='xgboost') +data(agaricus.test, package='xgboost') +train <- agaricus.train +test <- agaricus.test + +bst <- xgboost(data = train$data, label = train$label, max.depth = 2, + eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") + +test_that("dump works", { + capture.output(print(xgb.dump(bst))) +}) + + From d833038ba1e455b19e5f57be36acc8d624f5f6a9 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 21:48:57 -0400 Subject: [PATCH 2/8] TST: Added test for xgb.importance --- R-package/tests/testthat/test_helpers.R | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R index a7656f072..57f4c48fb 100644 --- a/R-package/tests/testthat/test_helpers.R +++ b/R-package/tests/testthat/test_helpers.R @@ -1,17 +1,27 @@ context('Test helper functions') require(xgboost) +require(data.table) +require(Matrix) +require(vcd) +data(Arthritis) data(agaricus.train, package='xgboost') -data(agaricus.test, package='xgboost') -train <- agaricus.train -test <- agaricus.test +df <- data.table(Arthritis, keep.rownames = F) +df[,AgeDiscret:= as.factor(round(Age/10,0))] +df[,AgeCat:= as.factor(ifelse(Age > 30, "Old", "Young"))] +df[,ID:=NULL] +sparse_matrix = sparse.model.matrix(Improved~.-1, data = df) +output_vector = df[,Y:=0][Improved == "Marked",Y:=1][,Y] +bst <- xgboost(data = sparse_matrix, label = output_vector, max.depth = 9, + eta = 1, nthread = 2, nround = 10,objective = "binary:logistic") -bst <- xgboost(data = train$data, label = train$label, max.depth = 2, - eta = 1, nthread = 2, nround = 2,objective = "binary:logistic") -test_that("dump works", { +test_that("xgb.dump works", { capture.output(print(xgb.dump(bst))) }) - +test_that("xgb.importance works", { + xgb.dump(bst, 'xgb.model.dump', with.stats = T) + importance <- xgb.importance(sparse_matrix@Dimnames[[2]], 'xgb.model.dump') +}) From 408c3a62a8255c84f777219c54b2f985e6c37bf7 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 21:49:27 -0400 Subject: [PATCH 3/8] TST: Added test for xgb.plot.tree --- R-package/tests/testthat/test_helpers.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R index 57f4c48fb..e26ec155b 100644 --- a/R-package/tests/testthat/test_helpers.R +++ b/R-package/tests/testthat/test_helpers.R @@ -25,3 +25,7 @@ test_that("xgb.importance works", { xgb.dump(bst, 'xgb.model.dump', with.stats = T) importance <- xgb.importance(sparse_matrix@Dimnames[[2]], 'xgb.model.dump') }) + +test_that("xgb.plot.tree works", { + xgb.plot.tree(agaricus.train$data@Dimnames[[2]], model = bst) +}) \ No newline at end of file From 886955148ddcc3cc711ff1307d4625f3406e3931 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 21:55:17 -0400 Subject: [PATCH 4/8] TST: Added test for models with custom objective --- .../tests/testthat/test_custom_objective.R | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 R-package/tests/testthat/test_custom_objective.R diff --git a/R-package/tests/testthat/test_custom_objective.R b/R-package/tests/testthat/test_custom_objective.R new file mode 100644 index 000000000..a941e39c8 --- /dev/null +++ b/R-package/tests/testthat/test_custom_objective.R @@ -0,0 +1,43 @@ +context('Test models with custom objective') + +require(xgboost) + +test_that("custom objective works", { + data(agaricus.train, package='xgboost') + data(agaricus.test, package='xgboost') + dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label) + dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label) + + watchlist <- list(eval = dtest, train = dtrain) + num_round <- 2 + + logregobj <- function(preds, dtrain) { + labels <- getinfo(dtrain, "label") + preds <- 1/(1 + exp(-preds)) + grad <- preds - labels + hess <- preds * (1 - preds) + return(list(grad = grad, hess = hess)) + } + evalerror <- function(preds, dtrain) { + labels <- getinfo(dtrain, "label") + err <- as.numeric(sum(labels != (preds > 0)))/length(labels) + return(list(metric = "error", value = err)) + } + + param <- list(max.depth=2, eta=1, nthread = 2, silent=1, + objective=logregobj, eval_metric=evalerror) + + bst <- xgb.train(param, dtrain, num_round, watchlist) + attr(dtrain, 'label') <- getinfo(dtrain, 'label') + + logregobjattr <- function(preds, dtrain) { + labels <- attr(dtrain, 'label') + preds <- 1/(1 + exp(-preds)) + grad <- preds - labels + hess <- preds * (1 - preds) + return(list(grad = grad, hess = hess)) + } + param <- list(max.depth=2, eta=1, nthread = 2, silent=1, + objective=logregobjattr, eval_metric=evalerror) + bst <- xgb.train(param, dtrain, num_round, watchlist) +}) \ No newline at end of file From 3a49e1bdb1c83c720a1cc1a471bcc9da7085b43c Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 21:56:50 -0400 Subject: [PATCH 5/8] TST: Added more checks for testing custom objective --- R-package/tests/testthat/test_custom_objective.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R-package/tests/testthat/test_custom_objective.R b/R-package/tests/testthat/test_custom_objective.R index a941e39c8..9fcbeca4d 100644 --- a/R-package/tests/testthat/test_custom_objective.R +++ b/R-package/tests/testthat/test_custom_objective.R @@ -28,6 +28,8 @@ test_that("custom objective works", { objective=logregobj, eval_metric=evalerror) bst <- xgb.train(param, dtrain, num_round, watchlist) + expect_equal(class(bst), "xgb.Booster") + expect_equal(length(bst$raw), 1064) attr(dtrain, 'label') <- getinfo(dtrain, 'label') logregobjattr <- function(preds, dtrain) { @@ -40,4 +42,6 @@ test_that("custom objective works", { param <- list(max.depth=2, eta=1, nthread = 2, silent=1, objective=logregobjattr, eval_metric=evalerror) bst <- xgb.train(param, dtrain, num_round, watchlist) + expect_equal(class(bst), "xgb.Booster") + expect_equal(length(bst$raw), 1064) }) \ No newline at end of file From c50cf6d7ff367ca40a4453f10d07edf569614f9f Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 22:03:28 -0400 Subject: [PATCH 6/8] TST: Added test for poisson regression --- R-package/tests/testthat/test_poisson_regression.R | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 R-package/tests/testthat/test_poisson_regression.R diff --git a/R-package/tests/testthat/test_poisson_regression.R b/R-package/tests/testthat/test_poisson_regression.R new file mode 100644 index 000000000..5d3d78e27 --- /dev/null +++ b/R-package/tests/testthat/test_poisson_regression.R @@ -0,0 +1,13 @@ +context('Test poisson regression model') + +require(xgboost) + +test_that("poisson regression works", { + data(mtcars) + bst = xgboost(data=as.matrix(mtcars[,-11]),label=mtcars[,11], + objective='count:poisson',nrounds=5) + expect_equal(class(bst), "xgb.Booster") + pred = predict(bst,as.matrix(mtcars[,-11])) + expect_equal(length(pred), 32) + sqrt(mean((pred-mtcars[,11])^2)) +}) \ No newline at end of file From fbf2a5feedb43e5d81ca6122b060b51565d7f1c9 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 22:49:10 -0400 Subject: [PATCH 7/8] DOC: Updated CONTRIBUTORS.md --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 6ae79f795..ab9c980c8 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -34,6 +34,7 @@ List of Contributors * [Zygmunt ZajÄ…c](https://github.com/zygmuntz) - Zygmunt is the master behind the early stopping feature frequently used by kagglers. * [Ajinkya Kale](https://github.com/ajkl) +* [Yuan Tang](https://github.com/terrytangyuan) * [Boliang Chen](https://github.com/cblsjtu) * [Vadim Khotilovich](https://github.com/khotilov) * [Yangqing Men](https://github.com/yanqingmen) From 33f1ab3ae1237f788f09e81a0106aff46869b2bd Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Mon, 7 Sep 2015 22:51:14 -0400 Subject: [PATCH 8/8] TST: Added one minor check for xgb.importance --- R-package/tests/testthat/test_helpers.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R index e26ec155b..4d80146e3 100644 --- a/R-package/tests/testthat/test_helpers.R +++ b/R-package/tests/testthat/test_helpers.R @@ -24,6 +24,7 @@ test_that("xgb.dump works", { test_that("xgb.importance works", { xgb.dump(bst, 'xgb.model.dump', with.stats = T) importance <- xgb.importance(sparse_matrix@Dimnames[[2]], 'xgb.model.dump') + expect_equal(dim(importance), c(7, 4)) }) test_that("xgb.plot.tree works", {