xgboard.dump: Xgboard Dumper

Description Usage Arguments Examples

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

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## 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]))
}

## End(Not run)

Laurae2/Laurae documentation built on May 8, 2019, 7:59 p.m.