Learn R Programming

Laurae (version 0.0.0.9001)

xgboard.dump: Xgboard Dumper

Description

This function performs dumping (logging) of the specified input metrics on the dump (log file). It resists file locks, but if the file lock is permanent, then this function will never stop (and xgboost will be interrupted).

Usage

xgboard.dump(metric, dump)

Arguments

metric
TYpe: numeric. A single value or a vector of numerics representing the metric.
dump
Type: environment. An environment created by xgboard.init.

Examples

Run this code
## Not run: ------------------------------------
# # First, we must load libraries: xgboost, data.table, and R.utils
# library(xgboost)
# library(data.table)
# library(R.utils)
# 
# # Second, we load some data
# data(agaricus.train, package='xgboost')
# data(agaricus.test, package='xgboost')
# 
# # Third, we create the xgb.DMatrices and the watchlist
# dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
# dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
# watchlist <- list(train = dtrain, eval = dtest)
# 
# # Fourth, we create a metric with Accuracy/Threshold logging
# xgboard.eval.error <- function(preds, dtrain, dump) {
# 
#   # Get xgboost label info
#   y_true <- getinfo(dtrain, "label")
#   
#   # Do stuff to get best acc + best threshold
#   DT <- data.table(y_true = y_true, y_prob = preds, key = "y_prob")
#   cleaner <- !duplicated(DT[, "y_prob"], fromLast = TRUE)
#   lens <- length(y_true)
#   nump <- sum(y_true)
#   DT[, tn_v := cumsum(y_true == 0)]
#   DT[, tp_v := nump - cumsum(y_true == 1)]
#   DT <- DT[cleaner, ]
#   DT[, acc := (tn_v + tp_v) / lens]
#   
#   # Stuff is stored here
#   best <- which.max(DT$acc)[1]
#   metric <- c(DT$acc[best], DT$y_prob[best])
#   
#   # Dump (log) both Accuracy and Threshold
#   xgboard.dump(metric, dump)
#   
#   # Return metric as typically done with xgboost custom evaluation metric
#   return(list(metric = "error", value = metric[1]))
# }
## ---------------------------------------------

Run the code above in your browser using DataLab