'Implementing threat score/critical success index objective in lightGBM
I'm interested in training a lightGBM binary classifier on a custom objective. I want to train it using "Threat Score" (aka "Critical Success Index"), which is simply True Positive / (True Positive + False Positive + False Negative).
The lightgbm package in R has the following example for using a custom objective function:
# Load in the agaricus dataset
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
dtrain <- lgb.Dataset(agaricus.train$data, label = agaricus.train$label)
dtest <- lgb.Dataset.create.valid(dtrain, data = agaricus.test$data, label = agaricus.test$label)
# Note: for customized objective function, we leave objective as default
# Note: what we are getting is margin value in prediction
# You must know what you are doing
param <- list(
num_leaves = 4L
, learning_rate = 1.0
)
valids <- list(eval = dtest)
num_round <- 20L
logregobj <- function(preds, dtrain) {
labels <- get_field(dtrain, "label")
preds <- 1.0 / (1.0 + exp(-preds))
grad <- preds - labels
hess <- preds * (1.0 - preds)
return(list(grad = grad, hess = hess))
}
evalerror <- function(preds, dtrain) {
labels <- get_field(dtrain, "label")
err <- as.numeric(sum(labels != (preds > 0.5))) / length(labels)
return(list(name = "error", value = err, higher_better = FALSE))
}
bst <- lgb.train(
param
, dtrain
, num_round
, valids
, obj = logregobj
, eval = evalerror
, early_stopping_round = 3L
)
What I'm interested in is the logregobj function. Instead of what's used in the example, I want to do something like the following, but I'm unsure what to use for the gradient and hessian values.
threat_score <- function(preds, dtrain) {
label <- get_field(dtrain, "label")
tp <- as.numeric(sum(as.integer(if_else(preds >= 0.5 & label == 1, 1, 0))))
fp <- as.numeric(sum(as.integer(if_else(preds >= 0.5 & label == 0, 1, 0))))
fn <- as.numeric(sum(as.integer(if_else(preds <= 0.5 & label == 1, 1, 0))))
hitrate <- tp/tp+fn
falserate <- fp / fp+tn
grad <- tp/(tp+fp+fn) # Threat score
hess <- sqrt((TS^2)*((1-hitrate)/(tp) + fp*(1-falserate)/((tp+fp+fn)^2))) #standard error
return(list(grad = grad, hess = hess ))
}
I've entered the threat score calculation for gradient and the standard error for hessian as a placeholder but I feel like I should probably calculate the second-order derivative for hessian.
Essentially I'm just trying to optimize for threat score rather than a more typical objective like logloss.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
