| 
 | 1 | + | 
 | 2 | +test_that("PipeOpClassWeightsEx - basic properties", {  | 
 | 3 | +  op = PipeOpClassWeightsEx$new()  | 
 | 4 | +  task = mlr_tasks$get("german_credit")  | 
 | 5 | +  expect_pipeop(op)  | 
 | 6 | +  train_pipeop(op, inputs = list(task))  | 
 | 7 | +  predict_pipeop(op, inputs = list(task))  | 
 | 8 | + | 
 | 9 | +  expect_datapreproc_pipeop_class(PipeOpClassWeights, task = task,  | 
 | 10 | +                                  predict_like_train = FALSE)  | 
 | 11 | +})  | 
 | 12 | + | 
 | 13 | +test_that("PipeOpClassWeightsEx - error for Tasks without weights property, #937", {  | 
 | 14 | +  skip_if_not_installed("mlr3learners")  | 
 | 15 | +  skip_if_not_installed("MASS")  | 
 | 16 | + | 
 | 17 | +  set.seed(1234)  | 
 | 18 | +  task = as_task_classif(data.table(  | 
 | 19 | +    y = factor(rep(c("A", "B", "A", "C"), 4)),  | 
 | 20 | +    x = runif(16)  | 
 | 21 | +  ), target = "y")  | 
 | 22 | + | 
 | 23 | +  # no error: Learner has weights property  | 
 | 24 | +  gr = po("classweightsex", param_vals = list(mapping = c("A" = 0.6, "B" = 0.3, "C" = 0.1))) %>>% lrn("classif.featureless")  | 
 | 25 | +  expect_no_error(gr$train(task))  | 
 | 26 | + | 
 | 27 | +  # error: Learner does not have weights property  | 
 | 28 | +  gr = po("classweightsex",  param_vals = list(mapping = c("A" = 0.6, "B" = 0.3, "C" = 0.1))) %>>% lrn("classif.lda")  | 
 | 29 | +  expect_error(gr$train(task), ".*Learner does not support weights.*")  | 
 | 30 | + | 
 | 31 | +  # no error: use_weights is set to "ignore"  | 
 | 32 | +  gr = po("classweightsex", param_vals = list(mapping = c("A" = 0.6, "B" = 0.4, "C" = 0.1))) %>>% lrn("classif.lda", use_weights = "ignore")  | 
 | 33 | +  expect_no_error(gr$train(task))  | 
 | 34 | + | 
 | 35 | +})  | 
 | 36 | + | 
 | 37 | +test_that("PipeOpClassWeightsEx", {  | 
 | 38 | + | 
 | 39 | +  task = mlr_tasks$get("iris")  | 
 | 40 | + | 
 | 41 | +  # Method inverse_class_frequency  | 
 | 42 | +  poicf = po("classweightsex", param_vals = list(weight_method = "inverse_class_frequency"))  | 
 | 43 | +  nt = poicf$train(list(task))[[1L]]  | 
 | 44 | +  expect_equal(nt$data(), task$data())  | 
 | 45 | + | 
 | 46 | + | 
 | 47 | +  # manual_weights = as.data.table(1 / table(task$data()$Species))  | 
 | 48 | +  # weights = if ("weights_learner" %in% names(nt)) "weights_learner" else "weights"  | 
 | 49 | +  # expect_equal(nt[[weights]]$weight, ifelse(nt$truth(nt[[weights]]$row_ids) == "neg", 1, 3))  | 
 | 50 | +})  | 
0 commit comments