/
test-FeatureImp.R
186 lines (163 loc) · 5.6 KB
/
test-FeatureImp.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
set.seed(42)
expected_colnames <- c(
"feature", "importance.05", "importance",
"importance.95", "permutation.error"
)
test_that("FeatureImp works for single output", {
var.imp <- FeatureImp$new(predictor1, loss = "mse")
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), ncol(X))
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
p <- plot(var.imp, sort = FALSE)
expect_s3_class(p, c("gg", "ggplot"))
p
p <- var.imp$plot()
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp works for single output with single repetition", {
var.imp <- FeatureImp$new(predictor1, loss = "mse", n.repetitions = 1)
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), ncol(X))
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp with difference", {
var.imp <- FeatureImp$new(predictor1, loss = "mse", compare = "difference")
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), ncol(X))
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp with 0 model error", {
data(iris)
require("mlr")
lrn <- mlr::makeLearner("classif.rpart", predict.type = "prob")
tsk <- mlr::makeClassifTask(data = iris, target = "Species")
mod <- mlr::train(lrn, tsk)
pred <- Predictor$new(mod, data = iris, y = iris$Species == "setosa", class = "setosa")
expect_warning(
{
var.imp <- FeatureImp$new(pred, loss = "mae")
},
"Model error is 0"
)
expect_equal(var.imp$compare, "difference")
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp works for single output and function as loss", {
var.imp <- FeatureImp$new(predictor1, loss = Metrics::mse)
dat <- var.imp$results
expect_class(dat, "data.frame")
# Making sure the result is sorted by decreasing importance
expect_equal(dat$importance, dat[order(dat$importance, decreasing = TRUE), ]$importance)
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), ncol(X))
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp works for multiple output", {
var.imp <- FeatureImp$new(predictor2, loss = "ce")
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), ncol(X))
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp fails without target vector", {
predictor2 <- Predictor$new(f, data = X, predict.fun = predict.fun)
expect_error(FeatureImp$new(predictor2, loss = "ce"))
})
test_that("Works for different repetitions.", {
var.imp <- FeatureImp$new(predictor1, loss = "mse", n.repetitions = 2)
dat <- var.imp$results
expect_class(dat, "data.frame")
})
test_that("Model receives data.frame without additional columns", {
# https://stackoverflow.com/questions/51980808/r-plotting-importance-feature-using-featureimpnew
library(mlr)
library(ranger)
data("iris")
tsk <- mlr::makeClassifTask(data = iris, target = "Species")
lrn <- mlr::makeLearner("classif.ranger", predict.type = "prob")
mod <- mlr:::train(lrn, tsk)
X <- iris[which(names(iris) != "Species")]
predictor <- Predictor$new(mod, data = X, y = iris$Species)
imp <- FeatureImp$new(predictor, loss = "ce")
expect_r6(imp)
})
set.seed(12)
X <- data.frame(x1 = 1:10, x2 = 1:10, x3 = 1:10)
y <- X[, 1] + X[, 2] + rnorm(10, 0, 0.1)
pred.fun <- function(newdata) {
newdata[, 1] + newdata[, 2]
}
pred <- Predictor$new(data = X, predict.fun = pred.fun, y = y)
test_that("Feature Importance 0", {
fimp <- FeatureImp$new(pred, loss = "mae", n.repetitions = 3)
expect_equal(fimp$results$importance[3], 1)
})
test_that("FeatureImp works for a subset of features", {
var.imp <- FeatureImp$new(predictor1, loss = "mse", features = c("a", "b"))
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), 2)
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("Invalid feature names are caught", {
expect_error(
FeatureImp$new(predictor1, loss = "mse", features = c("x", "y", "z")),
"failed: Must be a subset of {'a','b','c','d'}, but is {'x','y','z'}",
fixed = TRUE
)
})
test_that("FeatureImp works for groups of features", {
groups = list(ab = c("a", "b"), cd = c("c", "d"))
var.imp <- FeatureImp$new(predictor1, loss = "mse", features = groups)
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), 2)
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})
test_that("FeatureImp works for overlapping groups of features", {
groups = list(ab = c("a", "b"), bc = c("b", "c"))
var.imp <- FeatureImp$new(predictor1, loss = "mse", features = groups)
dat <- var.imp$results
expect_class(dat, "data.frame")
expect_false("data.table" %in% class(dat))
expect_equal(colnames(dat), expected_colnames)
expect_equal(nrow(dat), 2)
p <- plot(var.imp)
expect_s3_class(p, c("gg", "ggplot"))
p
})