Skip to content

Commit 7e5fcf2

Browse files
impelmented test architecture for comparing weight_method to manual calculation
1 parent 7771311 commit 7e5fcf2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

R/PipeOpClassWeightsEx.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ PipeOpClassWeightsEx = R6Class("PipeOpClassWeightsEx",
114114

115115
truth = task$truth()
116116

117-
class_frequency = table(truth) / length(truth)
117+
class_frequency = prop.table(table(truth))
118118
class_names = names(class_frequency)
119119

120120
weights_by_class = switch(pv$weight_method,

tests/testthat/test_pipeop_classweightsex.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ test_that("PipeOpClassWeightsEx", {
4343
nt = poicf$train(list(task))[[1L]]
4444
expect_equal(nt$data(), task$data())
4545

46+
freq = prop.table(table(task$truth()))
47+
manual_weights = 1 / freq[task$truth()]
48+
49+
if ("weights_learner" %in% names(nt$col_roles)) {
50+
computed_weights = nt$weights_learner
51+
} else {
52+
computed_weights = nt$weights
53+
}
54+
55+
expect_equal(computed_weights[["weight"]], as.numeric(unclass(manual_weights)))
56+
4657

47-
# manual_weights = as.data.table(1 / table(task$data()$Species))
4858
# weights = if ("weights_learner" %in% names(nt)) "weights_learner" else "weights"
4959
# expect_equal(nt[[weights]]$weight, ifelse(nt$truth(nt[[weights]]$row_ids) == "neg", 1, 3))
5060
})

0 commit comments

Comments
 (0)