-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers.R
48 lines (36 loc) · 1013 Bytes
/
helpers.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
# p = probability for levs[2] => matrix with probs for levs[1] and levs[2]
pvec2mat = function(p, levs) {
stopifnot(is.numeric(p))
y = matrix(c(1 - p, p), ncol = 2L, nrow = length(p))
colnames(y) = levs
y
}
ordered_features = function(task, learner) {
cols = names(learner$state$data_prototype) %??% learner$state$feature_names
task$data(cols = intersect(cols, task$feature_names))
}
as_numeric_matrix = function(x) { # for svm / #181
x = as.matrix(x)
if (is.logical(x)) {
storage.mode(x) = "double"
}
x
}
swap_levels = function(x) {
factor(x, levels = rev(levels(x)))
}
rename = function(x, old, new) {
if (length(x)) {
ii = match(names(x), old, nomatch = 0L)
names(x)[ii > 0L] = new[ii]
}
x
}
extract_loglik = function(self) {
require_namespaces(self$packages)
if (is.null(self$model)) {
stopf("Learner '%s' has no model stored", self$id)
}
stats::logLik(self$model)
}
opts_default_contrasts = list(contrasts = c("contr.treatment", "contr.poly"))