File: test_classif_neuralnet.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 (65 lines) | stat: -rwxr-xr-x 2,609 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

test_that("classif_neuralnet", {
  requirePackagesOrSkip("neuralnet", default.method = "load")

  set.seed(getOption("mlr.debug.seed"))
  # test with empty paramset
  capture.output({
    # neuralnet is not dealing with formula with `.` well
    nms = names(binaryclass.train)
    formula.head = as.character(binaryclass.formula)[2]
    varnames = nms[nms != formula.head]
    formula.head = paste("as.numeric(", formula.head, ")~")
    formula.expand = paste(formula.head, paste(varnames, collapse = "+"))
    formula.expand = as.formula(formula.expand)
    traindat = binaryclass.train
    traindat[[binaryclass.target]] = as.numeric(traindat[[binaryclass.target]]) - 1

    m = neuralnet::neuralnet(formula.expand,
      data = traindat, err.fct = "ce",
      linear.output = FALSE)
    p = neuralnet::compute(m,
      covariate = binaryclass.test[, -ncol(binaryclass.test)])
    p = as.numeric(as.vector(p[[2]]) > 0.5)
    p = factor(p, labels = binaryclass.class.levs)
  })

  set.seed(getOption("mlr.debug.seed"))
  testSimple("classif.neuralnet", binaryclass.df, binaryclass.target,
    binaryclass.train.inds, p,
    parset = list())

  # test with params passed
  capture.output({
    # neuralnet is not dealing with formula with `.` well
    nms = names(binaryclass.train)
    formula.head = as.character(binaryclass.formula)[2]
    varnames = nms[nms != formula.head]
    formula.head = paste("as.numeric(", formula.head, ")~")
    formula.expand = paste(formula.head, paste(varnames, collapse = "+"))
    formula.expand = as.formula(formula.expand)
    traindat = binaryclass.train
    traindat[[binaryclass.target]] = as.numeric(traindat[[binaryclass.target]]) - 1

    m = neuralnet::neuralnet(formula.expand,
      hidden = 7, data = traindat,
      err.fct = "ce", linear.output = FALSE)
    p = neuralnet::compute(m,
      covariate = binaryclass.test[, -ncol(binaryclass.test)])
    p = as.numeric(as.vector(p[[2]]) > 0.5)
    p = factor(p, labels = binaryclass.class.levs)
  })

  testSimple("classif.neuralnet", binaryclass.df, binaryclass.target,
    binaryclass.train.inds, p,
    parset = list(hidden = 7, err.fct = "ce"))

  # Neuralnet doesn't have the `predict` method
  #   set.seed(getOption("mlr.debug.seed"))
  #   lrn = makeLearner("classif.neuralnet",hidden=7)
  #   task = makeClassifTask(data = binaryclass.df, target = binaryclass.target)
  #   m2 = try(train(lrn, task, subset = binaryclass.train.inds))
  #   p2 = predictLearner(.learner=lrn,.model=m2,
  #                       .newdata = binaryclass.test[,-ncol(binaryclass.test)])
  #   expect_equal(p,p2,tol=1e-4)
})