Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weights reworked #76

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/r-cmd-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
config:
- {os: ubuntu-latest, r: 'devel'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'release', dev-package: 'mlr-org/mlr3@weights_reworked'}

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Suggests:
kableExtra,
fairml,
iml
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Config/testthat/edition: 3
Config/testthat/parallel: false
Roxygen: list(markdown = TRUE)
Expand Down
10 changes: 5 additions & 5 deletions R/PipeOpReweighing.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ PipeOpReweighingWeights = R6Class("PipeOpReweighingWeights",
wtab = compute_reweighing_weights(task, 1)
wcol = task$data(cols = c(task$backend$primary_key, task$target_names, task$col_roles$pta))
wcol = wcol[wtab, on = c(task$target_names, task$col_roles$pta)][, c(task$backend$primary_key, "wt"), with = FALSE]
if (is.null(task$weights)) {
if (is.null(task$weights_learner)) {
initial_weights = rep(1, task$nrow)
} else {
initial_weights = task$weights[wcol]$weight
initial_weights = task$weights_learner[wcol]$weight
}
wcol = wcol[, wt := wt * initial_weights]
setnames(wcol, c(task$backend$primary_key, weightcolname))
task$cbind(wcol)

if (length(task$col_roles$weight)) {
task$set_col_roles(task$col_roles$weight, remove_from = "weight")
if (length(task$col_roles$weights_learner)) {
task$set_col_roles(task$col_roles$weights_learner, remove_from = "weights_learner")
}

task$set_col_roles(weightcolname, "weight")
task$set_col_roles(weightcolname, "weights_learner")
task
},

Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test_pipeop_reweighing.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ test_that("reweighing_wts", {
skip_on_cran()
skip_if_not_installed("rpart")
tsk = po("reweighing_wts")$train(list(tsk("adult_train")$filter(1:300)))[[1]]
expect_true(tsk$col_roles$weight == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights)
expect_true(tsk$col_roles$weights_learner == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights_learner)
dt = dt[, mean(weight) * .N, by = .(sex, target)][, sum(V1 / sum(V1)), by = "target"]
expect_true(all(abs(dt$V1 - 1) < 1e-3))
})
Expand All @@ -35,15 +35,15 @@ test_that("reweighing_wts with initial weights", {
skip_if_not_installed("rpart")
t1 = tsk("compas")
t2 = t1$clone()
t2$set_col_roles("age", "weight")
t2$set_col_roles("age", "weights_learner")

p1 = po("reweighing_wts")
p2 = p1$clone()

ot1 = p1$train(list(t1))[[1]]
ot2 = p2$train(list(t2))[[1]]
w1 = ot1$weights$weight * t1$data(cols = "age")[["age"]]
w2 = ot2$weights$weight
w1 = ot1$weights_learner$weight * t1$data(cols = "age")[["age"]]
w2 = ot2$weights_learner$weight
expect_true(abs(mean(w1 - w2)) < 1e-2)
})

Expand All @@ -68,8 +68,8 @@ test_that("reweighing int to char conversion", {
t = TaskClassif$new("adult_int", backend = dt, target = "target")
t$col_roles$pta = "sex"
tsk = po("reweighing_wts")$train(list(t))[[1]]
expect_true(tsk$col_roles$weight == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights)
expect_true(tsk$col_roles$weights_learner == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights_learner)
dt = dt[, mean(weight) * .N, by = .(sex, target)][, sum(V1 / sum(V1)), by = "target"]
expect_true(all(abs(dt$V1 - 1) < 1e-3))

Expand All @@ -79,8 +79,8 @@ test_that("reweighing int to char conversion", {
t = TaskClassif$new("adult_int", backend = dt, target = "target")
t$col_roles$pta = "sex"
tsk = po("reweighing_wts")$train(list(t))[[1]]
expect_true(tsk$col_roles$weight == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights)
expect_true(tsk$col_roles$weights_learner == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights_learner)
dt = dt[, mean(weight) * .N, by = .(sex, target)][, sum(V1 / sum(V1)), by = "target"]
expect_true(all(abs(dt$V1 - 1) < 1e-3))

Expand All @@ -90,8 +90,8 @@ test_that("reweighing int to char conversion", {
t = TaskClassif$new("adult_int", backend = dt, target = "target")
t$col_roles$pta = "sex"
tsk = po("reweighing_wts")$train(list(t))[[1]]
expect_true(tsk$col_roles$weight == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights)
expect_true(tsk$col_roles$weights_learner == "reweighing.WEIGHTS")
dt = cbind(tsk$data(cols = c("..row_id", "sex", "target")), tsk$weights_learner)
dt = dt[, mean(weight) * .N, by = .(sex, target)][, sum(V1 / sum(V1)), by = "target"]
expect_true(all(abs(dt$V1 - 1) < 1e-3))
})
Loading