I would like to set the class.weights parameter using a Ranger classifier in MLR3. In the base Ranger package, the class.weights parameter takes a vector. When trying to set the same parameter in MLR3, I get an error.

# create a dummy dataset and try it with ranger 
library(ranger)
dt <- data.frame(x = runif(100), y = factor(sample(0:1, 100, replace = TRUE)))
rr <- ranger(y ~ x, data = dt, class.weights = c(0.5, 0.95))

This runs fine. Now trying with MLR3:

library(mlr3)
library(mlr3learners)
library(mlr3misc)

task = TaskClassif$new(id = "imbalanced", backend = dt ,target="y") 
learner = lrn("classif.ranger")

learner$param_set$values = insert_named(
  learner$param_set$values, list("class.weights" = c(0.05, 0.95))
)

This returns an error

Error in self$assert(xs) : Assertion on 'xs' failed: class.weights: Must have length 1

Checking learner$param_set shows that MLR3 is expecting a ParamDBL for class.weight, not a list, hence the error.

learner$param_set$class

However, if you give it a single value for the parameter, Ranger throws an error

learner$param_set$values = insert_named(
  learner$param_set$values, list("class.weights" = 0.05)
)
rr = resample(task, learner, rsmp("cv"), store_models = TRUE)

The error returned is the following:

Error in ranger::ranger(dependent.variable.name = task$target_names, data = task$data(), : Error: Number of class weights not equal to number of classes

How can I set this class.weights parameter in MLR3?

0

There are 0 answers