From 8b45ef07ca9b2f1b593b8f93279fe20f15f283d8 Mon Sep 17 00:00:00 2001 From: El Potaeto Date: Sun, 4 Jan 2015 11:21:39 +0100 Subject: [PATCH] build data.table from raw model data --- R-package/R/xgb.plot.tree.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 R-package/R/xgb.plot.tree.R diff --git a/R-package/R/xgb.plot.tree.R b/R-package/R/xgb.plot.tree.R new file mode 100644 index 000000000..3f60d598a --- /dev/null +++ b/R-package/R/xgb.plot.tree.R @@ -0,0 +1,24 @@ +require(DiagrammeR) +require(stringr) +require(data.table) +require(magrittr) +text <- readLines('xgb.model.dump') %>% str_trim(side = "both") +position <- str_match(text, "booster") %>% is.na %>% not %>% which %>% c(length(text)+1) + +extract <- function(x, pattern) str_extract(x, pattern) %>% str_split("=") %>% lapply(function(x) x[2]) %>% unlist %>% as.numeric + +#for(i in 1:(length(position)-1)){ +i=1 + cat(paste("\n",i,"\n")) + tree <- text[(position[i]+1):(position[i+1]-1)] + paste(tree, collapse = "\n") %>% cat +branch <- str_match(tree, "leaf") %>% is.na %>% tree[.] +id <- str_extract(branch, "\\d*:") %>% str_replace(":", "") %>% as.numeric +feature <- str_extract(branch, "\\[.*\\]") +yes <- extract(branch, "yes=\\d*") +no <- extract(branch, "no=\\d*") +missing <- extract(branch, "missing=\\d+") +gain <- extract(branch, "gain=\\d*\\.*\\d*") +cover <- extract(branch, "cover=\\d*\\.*\\d*") +dt <- data.table(ID = id, Feature = feature, Yes = yes, No = no, Missing = missing, Gain = gain, Cover = cover) +#}