0
$\begingroup$

I am currently using ranger for binary classification. My dataset is highly imbalanced (10:1). I went over the documentation, and it appeared to me that class.weights could be a good hyperparameter to tune. But I cannot find a proper documentation/examples on how to carry out grid search for this hyperparameter. Can anyone help me with this?

My code looks like the following right now:

# set up the grid for param searching
grid_search <-  expand.grid(
  .mtry =  seq(5, 30, by = 10),
  .min.node.size = seq(10, 40, by = 15),
  .sample.fraction = seq(0.5, 0.9, by = 0.2),
  .num.trees = seq(400, 1000, by = 200),
  .class.weights = c(0.5, 3), #How to have different weights?
  OOB_error = 0
)

for(i in 1:nrow(grid_search)) {
  rf_model <- ranger(
    formula         = as.factor(trainY$sale) ~ .,
data            = trainX,
num.trees       = grid_search$.num.trees[i],
    mtry            = grid_search$.mtry[i],
min.node.size   = grid_search$.min.node.size[i],
    sample.fraction = grid_search$.sample.fraction[i],
probability     = TRUE,
class.weights   = c(0.5, 3),
seed            = 123
  )
  # add OOB error to grid
  grid_search$OOB_error[i] <- sqrt(rf_model$prediction.error)
  print(grid_search[i,])
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.