forked from mlr-org/mlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_classif_fda_classiFunc.kernel.R
77 lines (56 loc) · 2.54 KB
/
test_classif_fda_classiFunc.kernel.R
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
66
67
68
69
70
71
72
73
74
75
76
77
test_that("classif_classiFunc.kernel behaves like original api", {
requirePackagesOrSkip("classiFunc", default.method = "load")
data(ArrowHead, package = "classiFunc")
arrow.head = ArrowHead
classes = arrow.head[, "target"]
arrow.head = arrow.head[, colnames(arrow.head) != "target"]
test.inds = sample(1:nrow(arrow.head), size = 0.8 * nrow(arrow.head), replace = FALSE)
test.inds = (1:nrow(arrow.head))[!(1:nrow(arrow.head)) %in% test.inds]
mlearn = arrow.head[test.inds, ]
glearn = classes[test.inds]
mtest = arrow.head[test.inds, ]
gtest = classes[test.inds]
# classiFunc implementation
a1 = classiFunc::classiKernel(glearn, mlearn, h = 1, nderiv = 1)
p1 = predict(a1, mtest)
p2 = predict(a1, mlearn)
p1.prob = predict(a1, mtest, predict.type = "prob")
p2.prob = predict(a1, mlearn, predict.type = "prob")
#------------------------------------------------------------------------------
# getting the data ready for mlr
ph = as.data.frame(mlearn)
ph[, "label"] = glearn
phtst = as.data.frame(mtest)
phtst[, "label"] = gtest
# mlr interface
lrn = makeLearner("classif.classiFunc.kernel", h = 1, nderiv = 1)
fdata = makeFunctionalData(ph, fd.features = NULL, exclude.cols = "label")
ftest = makeFunctionalData(phtst, fd.features = NULL, exclude.cols = "label")
task = makeClassifTask(data = fdata, target = "label")
m = train(lrn, task)
cp = predict(m, newdata = ftest)
cp = unlist(cp$data$response, use.names = FALSE)
cp2 = predict(m, newdata = fdata)
cp2 = unlist(cp2$data$response, use.names = FALSE)
# check if the output from the original API matches the mlr learner's output
expect_equal(as.character(cp2), as.character(p2))
expect_equal(as.character(cp), as.character(p1))
#------------------------------------------------------------------------------
# test that predict.type = "prob" works
lrn.prob = makeLearner("classif.classiFunc.kernel",
h = 1,
nderiv = 1,
predict.type = "prob")
m.prob = train(lrn.prob, task)
cp.prob = predict(m.prob, newdata = ftest)
cp2.prob = predict(m.prob, newdata = fdata)
expect_equal(class(cp.prob)[1], "PredictionClassif")
expect_equal(as.matrix(getPredictionProbabilities(cp2.prob)), p2.prob)
expect_equal(as.matrix(getPredictionProbabilities(cp.prob)), p1.prob)
})
test_that("resampling classiFunc.kernel", {
requirePackagesOrSkip("classiFunc", default.method = "load")
lrn = makeLearner("classif.classiFunc.kernel", predict.type = "prob")
r = resample(lrn, fda.binary.gp.task.small, cv2)
expect_class(r, "ResampleResult")
})