File: test_tune_tuneThreshold.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 (47 lines) | stat: -rwxr-xr-x 1,676 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
36
37
38
39
40
41
42
43
44
45
46
47

test_that("tuneThreshold", {
  requirePackagesOrSkip("lhs", default.method = "load")

  # binary classes, 1 th
  lrn = makeLearner("classif.lda", predict.type = "prob")
  m = train(lrn, binaryclass.task)
  p = predict(m, binaryclass.task)
  tr = tuneThreshold(p)
  expect_true(length(tr$th) == 1 && tr$th >= 0 && tr$th <= 1)
  expect_true(tr$perf >= 0 && tr$perf < 0.3)

  # multiclass
  m = train(lrn, multiclass.task)
  p = predict(m, multiclass.task)
  tr = tuneThreshold(p, mmce, control = list(maxit = 5L))
  expect_true(length(tr$th) == 3 && all(tr$th >= 0) && all(tr$th <= 1))
  expect_true(tr$perf >= 0 && tr$perf < 0.1)
})

test_that("tuneThreshold works with all tuning methods", {
  lrn = makeLearner("classif.lda", predict.type = "prob")
  ps = makeParamSet(makeNumericParam("nu", lower = 2, upper = 3))
  ctrls = list(
    gensa = makeTuneControlGenSA(
      start = list(nu = 2.5), maxit = 1,
      tune.threshold = TRUE),
    cmaes = makeTuneControlCMAES(
      start = list(nu = 2.5), maxit = 1,
      tune.threshold = TRUE),
    design = makeTuneControlDesign(
      design = generateDesign(n = 2, par.set = ps),
      tune.threshold = TRUE),
    grid = makeTuneControlGrid(resolution = 2L, tune.threshold = TRUE),
    irace = makeTuneControlIrace(
      maxExperiments = 12, nbIterations = 1L,
      minNbSurvival = 1, tune.threshold = TRUE)
  )
  for (ctrl in ctrls) {
    lrn.tuned = makeTuneWrapper(lrn,
      resampling = cv2, measures = acc,
      par.set = ps, control = ctrl)
    res = resample(lrn.tuned, binaryclass.task,
      resampling = makeResampleDesc("Holdout"), extract = getTuneResult)
    expect_number(res$extract[[1]]$threshold)
  }
})