/
auto-ml.R
371 lines (340 loc) · 10.4 KB
/
auto-ml.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#' Tools for working with H2O AutoML results
#'
#' @description
#' Functions that returns a tibble describing model performances.
#'
#' * `rank_results()` ranks average cross validation performances
#' of candidate models on each metric.
#'
#' * `collect_metrics()` computes average statistics of performance metrics
#' (summarized) for each model, or raw value in each resample (unsummarized).
#'
#' * `tidy()` computes average performance for each model.
#'
#' * `member_weights()` computes member importance for stacked ensemble
#' models, i.e., the relative importance of base models in the meta-learner.
#' This is typically the coefficient magnitude in the second-level GLM model.
#'
#' `extract_fit_engine()` extracts single candidate model from `auto_ml()`
#' results. When `id` is null, it returns the leader model.
#'
#' `refit()` re-fits an existing AutoML model to add more candidates. The model
#' to be re-fitted needs to have engine argument `save_data = TRUE`, and
#' `keep_cross_validation_predictions = TRUE` if stacked ensembles is needed for
#' later models.
#'
#' @details
#' H2O associates with each model in AutoML an unique id. This can be used for
#' model extraction and prediction, i.e., `extract_fit_engine(x, id = id)`
#' returns the model and `predict(x, id = id)` will predict for that model.
#' `extract_fit_parsnip(x, id = id)` wraps the h2o model with parsnip
# classes to enable predict and print methods, other usage of this "fake"
#' parsnip model object is discouraged.
#'
#' The `algorithm` column corresponds to the model family H2O use for a
#' particular model, including xgboost (`"XGBOOST"`),
#' gradient boosting (`"GBM"`), random forest and variants (`"DRF"`, `"XRT"`),
#' generalized linear model (`"GLM"`), and neural network (`"deeplearning"`).
#' See the details section in [h2o::h2o.automl()] for more information.
#'
#' @param object,x A fitted `auto_ml()` model or workflow.
#' @param n An integer for the number of top models to extract from AutoML
#' results, default to all.
#' @param id A character vector of model ids to retrieve.
#' @param ... Not used.
#' @return A [tibble::tibble()].
#' @examplesIf agua:::should_run_examples()
#' if (h2o_running()) {
#' auto_fit <- auto_ml() %>%
#' set_engine("h2o", max_runtime_secs = 5) %>%
#' set_mode("regression") %>%
#' fit(mpg ~ ., data = mtcars)
#'
#' rank_results(auto_fit, n = 5)
#' collect_metrics(auto_fit, summarize = FALSE)
#' tidy(auto_fit)
#' member_weights(auto_fit)
#' }
#'
#' @export
#' @rdname automl-tools
rank_results.workflow <- function(x, ...) {
rank_results(hardhat::extract_fit_parsnip(x), ...)
}
#' @rdname automl-tools
#' @export
rank_results._H2OAutoML <- function(x, ...) {
rank_results(x$fit, ...)
}
#' @rdname automl-tools
#' @export
rank_results.H2OAutoML <- function(x,
n = NULL,
id = NULL,
...) {
leaderboard <- get_leaderboard(x, n, id)
models <- purrr::map(leaderboard$model_id, h2o_get_model)
cv_metrics <- purrr::map_dfr(models, get_cv_metrics, summarize = TRUE)
res <- cv_metrics %>%
dplyr::left_join(metric_info, by = ".metric") %>%
dplyr::group_by(.metric) %>%
dplyr::mutate(rank = rank(mean * direction, ties.method = "random")) %>%
dplyr::select(-direction) %>%
dplyr::ungroup()
res
}
get_cv_metrics <- function(x, summarize = TRUE) {
cv_summary <- x@model$cross_validation_metrics_summary
cv_summary[["sd"]] <- NULL
cv_summary[["mean"]] <- NULL
res <- tibble::as_tibble(cv_summary) %>%
dplyr::mutate(
id = x@model_id,
algorithm = id_to_algorithm(id),
.metric = rownames(cv_summary),
.before = 1
) %>%
tidyr::pivot_longer(dplyr::starts_with("cv"),
names_to = "cv_id",
values_to = "value"
)
if (summarize) {
res <- res %>%
dplyr::group_by(id, algorithm, .metric) %>%
dplyr::summarize(
mean = mean(value, na.rm = TRUE),
.groups = "drop"
)
}
res
}
metric_info <- tibble::tribble(
~.metric, ~direction,
"mae", 1,
"mean_residual_deviance", 1,
"mse", 1,
"residual_deviance", 1,
"rmse", 1,
"rmsle", 1,
"null_deviance", 1,
"r2", -1,
"logloss", 1,
"err", 1,
"err_count", 1,
"max_per_class_error", 1,
"mean_per_class_error", 1,
"recall", -1,
"accuracy", -1,
"auc", -1,
"f0point5", -1,
"f1", -1,
"f2", -1,
"lift_top_group", -1,
"mcc", -1,
"mean_per_class_accuracy", -1,
"pr_auc", -1,
"precision", -1,
"specificity", -1
)
check_automl_fit <- function(x) {
if (!inherits(x, "_H2OAutoML")) {
msg <- paste0(
"The first argument should be a fitted ",
"`auto_ml()` model or workflow."
)
rlang::abort(msg)
}
invisible(x)
}
#' @rdname automl-tools
#' @export
collect_metrics.workflow <- function(x, ...) {
collect_metrics(extract_fit_parsnip(x), ...)
}
#' @rdname automl-tools
#' @export
collect_metrics._H2OAutoML <- function(x, ...) {
collect_metrics(x$fit, ...)
}
#' @param summarize A logical; should metrics be summarized over resamples
#' (TRUE) or return the values for each individual resample.
#' @rdname automl-tools
#' @export
collect_metrics.H2OAutoML <- function(x,
summarize = TRUE,
n = NULL,
id = NULL,
...) {
leaderboard <- get_leaderboard(x, n = n, id = id)
lvl <- leaderboard$model_id
models <- purrr::map(leaderboard$model_id, h2o_get_model)
cv_metrics <- purrr::map_dfr(models, get_cv_metrics, summarize = FALSE)
if (summarize) {
res <- cv_metrics %>%
dplyr::mutate(id = factor(id, levels = lvl)) %>%
dplyr::group_by(id, algorithm, .metric) %>%
dplyr::summarize(
mean = mean(value, na.rm = TRUE),
std_err = sd(value) / sqrt(sum(!is.na(value))),
n = sum(!is.na(value)),
.groups = "drop"
) %>%
dplyr::mutate(id = as.character(id))
} else {
res <- cv_metrics %>%
dplyr::rename(.estimate = value)
}
res
}
#' @rdname automl-tools
#' @param keep_model A logical value for if the actual model object
#' should be retrieved from the server. Defaults to `TRUE`.
#' @export
tidy._H2OAutoML <- function(x,
n = NULL,
id = NULL,
keep_model = TRUE,
...) {
leaderboard <- get_leaderboard(x, n, id)
leaderboard <- leaderboard %>%
tidyr::pivot_longer(-c(model_id),
names_to = ".metric",
values_to = "mean"
) %>%
dplyr::rename(id = model_id) %>%
tidyr::nest(.metric = c(.metric, mean)) %>%
dplyr::ungroup()
if (!keep_model) {
return(leaderboard)
}
leaderboard %>%
dplyr::mutate(.model = purrr::map(
id,
~ extract_fit_parsnip(x, .x),
)) %>%
dplyr::mutate(
algorithm = purrr::map_chr(id, id_to_algorithm),
.after = 1
)
}
#' @rdname automl-tools
#' @export
get_leaderboard <- function(x, n = NULL, id = NULL) {
if (inherits(x, "_H2OAutoML")) {
x <- x$fit
}
leaderboard <- as.data.frame(x@leaderboard)
if (!is.null(id) && is.character(id)) {
n <- NULL
leaderboard <- leaderboard %>% dplyr::filter(model_id %in% id)
}
if (!is.null(n)) {
n <- check_leaderboard_n(leaderboard, n)
leaderboard <- leaderboard[seq_len(n), ]
}
tibble::as_tibble(leaderboard)
}
#' @rdname automl-tools
#' @export
member_weights <- function(x, ...) {
check_automl_fit(x)
leaderboard <- get_leaderboard(x)
model_id <- leaderboard[grep("StackedEnsemble", leaderboard$model_id), ]$model_id
ranks <- match(model_id, leaderboard$model_id)
tibble::tibble(
ensemble_id = model_id,
rank = ranks,
importance = purrr::map(ensemble_id, get_stacking_imp)
)
}
get_stacking_imp <- function(id) {
mod <- h2o_get_model(id)
meta_learner <- h2o_get_model(mod@model$metalearner$name)
res <- tibble::as_tibble(h2o::h2o.varimp(meta_learner))
res %>%
dplyr::rename(member = variable) %>%
dplyr::mutate(algorithm = id_to_algorithm(member)) %>%
tidyr::pivot_longer(-c(member, algorithm), names_to = "type", values_to = "value")
}
check_leaderboard_n <- function(leaderboard, n) {
n_models <- nrow(leaderboard)
if (!is.null(n) && n > n_models) {
msg <- paste0(
"`n` is larger than the number of models, ",
"returning all."
)
rlang::warn(msg)
}
min(n, n_models)
}
#' @export
#' @rdname automl-tools
extract_fit_parsnip._H2OAutoML <- function(x, id = NULL, ...) {
# for bundled objects, leaders are already extracted
if (!"leader" %in% methods::slotNames(x$fit)) {
mod <- x$fit
} else {
if (is.null(id)) {
id <- x$fit@leader@model_id
}
mod <- h2o_get_model(id)
leaderboard <- get_leaderboard(x)
automl_rank <- match(id, leaderboard$model_id)
attr(mod, "automl_rank") <- automl_rank
}
mod <- convert_h2o_parsnip(mod, x$spec, x$lvl, extra_class = NULL)
class(mod) <- c("h2o_fit", "H2OAutoML_fit", class(mod))
mod
}
#' @export
#' @rdname automl-tools
extract_fit_engine._H2OAutoML <- function(x, id = NULL, ...) {
# for bundled objects, leaders are already extracted
if (!"leader" %in% methods::slotNames(x$fit)) {
return(x$fit)
}
if (is.null(id)) {
id <- x$fit@leader@model_id
}
mod <- h2o_get_model(id)
mod
}
#' @export
#' @rdname automl-tools
refit.workflow <- function(object, ...) {
refit(extract_fit_parsnip(object), ...)
}
#' @export
#' @param verbosity Verbosity of the backend messages printed during training;
#' Must be one of NULL (live log disabled), "debug", "info", "warn", "error".
#' Defaults to NULL.
#' @rdname automl-tools
refit._H2OAutoML <- function(object, verbosity = NULL, ...) {
x <- object$fit
params <- x@leader@allparameters
project_name <- x@project_name
training_frame <- h2o_get_frame(params$training_frame)
if (is.null(training_frame)) {
msg <- paste0(
"The model needs to be trained with `save_data = TRUE` to ",
"enable re-fitting. If you want to train stacked ensembles in re-fitting, ",
"set `keep_cross_validation_predictions = TRUE` as well."
)
rlang::abort(msg)
}
x_names <- params$x
y <- params$y
cl <- rlang::call2(
"h2o.automl",
.ns = "h2o",
x = quote(x_names),
y = y,
training_frame = quote(training_frame),
project_name = project_name,
verbosity = verbosity,
...
)
res <- h2o::h2o.no_progress(rlang::eval_tidy(cl))
object$fit <- res
object
}