Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.