Created
December 4, 2019 01:22
-
-
Save topepo/27ce0096887ab95f4b615b90e91f8613 to your computer and use it in GitHub Desktop.
(try to) determine the default values for parsnip model arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_default_values <- function(model, engine, mode) { | |
eng <- engine | |
mod_args <- | |
parsnip::get_from_env(paste0(model, "_args")) %>% | |
dplyr::filter(engine == eng) | |
model_fun <- | |
parsnip::get_from_env(paste0(model, "_fit")) %>% | |
dplyr::filter(engine == eng) %>% | |
dplyr::mutate(pkg = purrr::map_chr(value, ~ .x$func["pkg"]), | |
fun = purrr::map_chr(value, ~ .x$func["fun"])) | |
if (nrow(model_fun) > 1) { | |
if (rlang::is_missing(mode)) { | |
rlang::abort("Please select a mode.") | |
} | |
md <- mode | |
model_fun <- | |
model_fun %>% | |
dplyr::filter(mode == md) | |
} | |
model_func <- utils::getFromNamespace(model_fun$fun, ns = model_fun$pkg) | |
model_info <- formals(model_func) | |
model_info <- model_info[mod_args$original] | |
model_info <- purrr::map(model_info, ~ if(is.null(.x)) "<cannot determine>" else .x) | |
model_defaults <- format(model_info) | |
mod_args$default <- model_defaults | |
mod_args %>% dplyr::select(engine, parsnip, original, default) | |
} | |
get_default_values("boost_tree", "C5.0") | |
get_default_values("boost_tree", "spark", "regression") | |
get_default_values("decision_tree", "rpart", "regression") | |
get_default_values("mlp", "keras", "regression") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment