-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathcreate_predict_fun.R
161 lines (144 loc) · 4.96 KB
/
create_predict_fun.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
create_predict_fun <- function(model, task, predict.fun = NULL, type = NULL) {
# Use user-specified predict.fun if user has passed one
# TODO: this might be useful also for all the other create_predict_fun methods as users might want to do specific things
if (is.null(model)) {
return(function(newdata) {
pred <- predict.fun(newdata = newdata)
if (is_label(pred)) {
factor_to_dataframe(pred)
}
data.frame(pred, check.names = FALSE)
})
}
if (!is.null(predict.fun)) {
return(function(newdata) sanitizePrediction(predict.fun(model, newdata = newdata)))
}
create_predict_fun2(model, task, predict.fun, type)
}
create_predict_fun2 <- function(model, task, predict.fun = NULL, type = NULL) {
UseMethod("create_predict_fun2")
}
create_predict_fun2.WrappedModel <- function(model, task, predict.fun = NULL, type = NULL) {
if (!requireNamespace("mlr")) {
"Please install the mlr package."
}
if (task == "classification") {
function(newdata) {
pred <- predict(model, newdata = newdata)
if (model$learner$predict.type == "response") {
pred <- mlr::getPredictionResponse(pred)
factor_to_dataframe(pred)
} else {
mlr::getPredictionProbabilities(pred, cl = model$task.desc$class.levels)
}
}
} else if (task == "regression") {
function(newdata) {
pred <- predict(model, newdata = newdata)
data.frame(.prediction = mlr::getPredictionResponse(pred))
}
} else {
stop(sprintf("Task type '%s' not supported", task))
}
}
create_predict_fun2.Learner <- function(model, task, predict.fun = NULL, type = NULL) {
if (!requireNamespace("mlr3")) {
"Please install the mlr3 package."
}
if (task == "classification") {
function(newdata) {
if (model$predict_type == "response") {
pred <- predict(model, newdata = newdata)
factor_to_dataframe(pred)
} else {
data.frame(predict(model, newdata = newdata, predict_type = "prob"), check.names = FALSE)
}
}
} else if (task == "regression") {
function(newdata) {
data.frame(.prediction = predict(model, newdata = newdata))
}
} else {
stop(sprintf("Task type '%s' not supported", task))
}
}
create_predict_fun2.train <- function(model, task, predict.fun = NULL, type = NULL) {
if (task == "classification") {
function(newdata) {
if (is.null(type)) {
pred <- predict(model, newdata = newdata)
} else {
pred <- predict(model, newdata = newdata, type = type)
}
if (is_label(pred)) {
pred <- factor_to_dataframe(pred)
}
pred
}
} else if (task == "regression") {
function(newdata) {
if (is.null(type)) {
prediction <- predict(model, newdata = newdata)
} else {
prediction <- predict(model, newdata = newdata, type = type)
}
data.frame(.prediction = prediction, check.names = FALSE)
}
} else {
stop(sprintf("task of type %s not allowed.", task))
}
}
#' @importFrom stats model.matrix
create_predict_fun2.default <- function(model, task, predict.fun = NULL, type = NULL) {
if (is.null(predict.fun)) {
if (is.null(type)) {
predict.fun <- function(object, newdata) predict(object, newdata)
} else {
predict.fun <- function(object, newdata) predict(object, newdata, type = type)
}
}
function(newdata) {
pred <- do.call(predict.fun, list(model, newdata = newdata))
if (is_label(pred)) {
pred <- factor_to_dataframe(pred)
}
data.frame(pred, check.names = FALSE)
}
}
create_predict_fun2.keras.engine.training.Model <- function(model, task, predict.fun = NULL, type = NULL) {
if (is.null(predict.fun)) {
predict.fun <- function(object, newdata) predict(object, newdata)
}
function(newdata) {
pred <- do.call(predict.fun, list(model, newdata = as.matrix(newdata)))
data.frame(pred, check.names = FALSE)
}
}
create_predict_fun2.H2ORegressionModel <- function(model, task, predict.fun = NULL, type = NULL) {
function(newdata) {
newdata2 <- h2o::as.h2o(newdata)
as.data.frame(h2o::h2o.predict(model, newdata = newdata2))
}
}
create_predict_fun2.H2OBinomialModel <- function(model, task, predict.fun = NULL, type = NULL) {
function(newdata) {
# TODO: Include predict.fun and type
newdata2 <- h2o::as.h2o(newdata)
as.data.frame(h2o::h2o.predict(model, newdata = newdata2))[, -1]
}
}
create_predict_fun2.H2OMultinomialModel <- function(model, task, predict.fun = NULL, type = NULL) {
function(newdata) {
# TODO: Include predict.fun and type
newdata2 <- h2o::as.h2o(newdata)
# Removes first column with classification
# Following columns contain the probabilities
as.data.frame(h2o::h2o.predict(model, newdata = newdata2))[, -1]
}
}
factor_to_dataframe <- function(fac) {
check_vector(fac)
res <- data.frame(model.matrix(~ fac - 1, data.frame(fac = fac), sep = ":"), check.names = FALSE)
colnames(res) <- substring(colnames(res), 4) #make.names(levels(fac))#
res
}