mlr-org / mlr3

mlr3: Machine Learning in R - next generation
https://mlr3.mlr-org.com
GNU Lesser General Public License v3.0
948 stars 85 forks source link

Class weights no longer work #1120

Closed larskotthoff closed 2 months ago

larskotthoff commented 3 months ago

Setting class weights results in an error (code from Chapter 2 of the book):

cancer_unweighted = tsk("breast_cancer")
summary(cancer_unweighted$data()$class)

# add column where weight is 2 if class "malignant", and 1 otherwise
df = cancer_unweighted$data()
df$weights = ifelse(df$class == "malignant", 2, 1)

# create new task and role
cancer_weighted = as_task_classif(df, target = "class")
cancer_weighted$set_col_roles("weights", roles = "weight")

# compare weighted and unweighted predictions
split = partition(cancer_unweighted)
lrn_rf = lrn("classif.ranger")
lrn_rf$train(cancer_unweighted, split$train)$
  predict(cancer_unweighted, split$test)$score()
lrn_rf$train(cancer_weighted, split$train)$
  predict(cancer_weighted, split$test)$score()

The last line gives the following error:

Error in task_set_roles(private$.col_roles, cols, roles, add_to, remove_from) : 
  Assertion on 'roles' failed: Must be a subset of {'feature','target','name','order','stratum','group','weights_learner','weights_measure','weights_resampling'}, but has additional elements {'weight'}.
> traceback()
7: stop(simpleError(sprintf(msg, ...), call.))
6: mstop("Assertion on '%s' failed: %s.", var.name, res, call. = sys.call(-2L))
5: makeAssertion(x, res, .var.name, add)
4: assert_subset(roles, names(li))
3: task_set_roles(private$.col_roles, cols, roles, add_to, remove_from)
2: .__Task__set_col_roles(self = self, private = private, super = super, 
       cols = cols, roles = roles, add_to = add_to, remove_from = remove_from)
1: cancer_weighted$set_col_roles("weights", roles = "weight")
larskotthoff commented 2 months ago

Seems to be fixed.