File: test_base_blocking.R

package info (click to toggle)
r-cran-mlr 2.19.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,392 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (35 lines) | stat: -rwxr-xr-x 1,124 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

test_that("blocking", {
  df = multiclass.df
  b = as.factor(rep(1:30, 5))
  ct = makeClassifTask(target = multiclass.target, data = multiclass.df, blocking = b)
  expect_true(getTaskDesc(ct)$has.blocking)

  res = makeResampleInstance(makeResampleDesc("CV", iters = 3, blocking.cv = TRUE), task = ct)
  for (j in 1:res$desc$iters) {
    train.j = res$train.inds[[j]]
    test.j = res$test.inds[[j]]
    tab = table(b[train.j])
    expect_true(setequal(c(0, 5), unique(as.numeric(tab))))
    tab = table(b[test.j])
    expect_true(setequal(c(0, 5), unique(as.numeric(tab))))
  }

  # test blocking in resample
  lrn = makeLearner("classif.lda")
  mycheck = function(rdesc, p, b) {
    for (j in 1:rdesc$iters) {
      test.j = p$data[p$data$iter == j, "id"]
      tab = table(b[test.j])
      expect_true(setequal(c(0, 5), unique(as.numeric(tab))))
    }
  }

  rdesc = makeResampleDesc("CV", iters = 3, blocking.cv = TRUE)
  p = resample(lrn, ct, rdesc)$pred
  mycheck(rdesc, p, b)

  rdesc = makeResampleDesc("RepCV", folds = 3, reps = 2, blocking.cv = TRUE)
  p = resample(lrn, ct, rdesc)$pred
  mycheck(rdesc, p, b)
})