Skip to content

Instantly share code, notes, and snippets.

@jtilly
Last active August 23, 2017 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtilly/dcb5a46a2fb9ca9bbac5c92e9925ed7f to your computer and use it in GitHub Desktop.
Save jtilly/dcb5a46a2fb9ca9bbac5c92e9925ed7f to your computer and use it in GitHub Desktop.
library(xgboost)
set.seed(1234)
N = 1000
x1 <- runif(N)
x <- ifelse(x1 <= 0.2, as.numeric(NA), x1)
y <- as.numeric(x1 >= 0.9)
bst <- xgboost(data = matrix(x, ncol=1), label = y,
objective = "binary:logistic", eval_metric = "logloss",
nrounds = 1, max_depth = 1, eta = 1., lambda = 0, nthread = 1)
# this shows the two possible leaf values
xgb.dump(model=bst)
pr <- function(xcolumn) {
md <- matrix(as.numeric(xcolumn), ncol=1)
ms <- as(md, "dgCMatrix")
cat('margin from dense : ', predict(bst, xgb.DMatrix(md), outputmargin=T), '\n')
cat('margin from sparse: ', predict(bst, xgb.DMatrix(ms), outputmargin=T), '\n')
#print(c('leaf from dense : ', predict(bst, xgb.DMatrix(md), predleaf=T), '\n'))
#print(c('leaf from sparse: ', predict(bst, xgb.DMatrix(ms), predleaf=T), '\n'))
}
# final row not NA
pr(c(NA,1))
pr(c(1,NA,1))
# final row NA
pr(c(NA))
pr(c(NA,NA))
pr(c(NA,NA,NA))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment