Created
March 25, 2020 01:03
-
-
Save topepo/582dcaf3b797a8323248101c5e0ea067 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
library(tidymodels) | |
library(rlang) | |
get_arg <- function(ns, f, arg) { | |
args <- formals(getFromNamespace(f, ns)) | |
args <- args %>% as.list() | |
as.character(args[[arg]]) | |
} | |
# Make the defaults character because there are cases where we will write something | |
# eg glmnet::glmnet would have "lambda (all)" or something similar | |
dt_defaults <- | |
tibble::tribble( | |
~model, ~engine, ~parsnip, ~original, ~default, | |
"decision_tree", "rpart", "tree_depth", "maxdepth", get_arg("rpart", "rpart.control", "maxdepth"), | |
"decision_tree", "rpart", "min_n", "minsplit", get_arg("rpart", "rpart.control", "minsplit"), | |
"decision_tree", "rpart", "cost_complexity", "cp", get_arg("rpart", "rpart.control", "cp"), | |
"decision_tree", "C5.0", "min_n", "minCases", get_arg("C50", "C5.0Control", "minCases"), | |
"decision_tree", "spark", "tree_depth", "max_depth", get_arg("sparklyr", "ml_decision_tree", "max_depth"), | |
"decision_tree", "spark", "min_n", "min_instances_per_node", get_arg("sparklyr", "ml_decision_tree", "min_instances_per_node"), | |
) | |
# emulating convert_args("decision_tree") | |
model_name <- "decision_tree" | |
envir <- get_model_env() | |
args <- | |
ls(envir) %>% | |
tibble::tibble(name = .) %>% | |
dplyr::filter(grepl("args", name)) %>% | |
dplyr::mutate(model = sub("_args", "", name), | |
args = purrr::map(name, ~envir[[.x]])) %>% | |
dplyr::filter(grepl(model_name, model)) %>% | |
tidyr::unnest(args) %>% | |
dplyr::select(model:original) %>% | |
full_join(dt_defaults) %>% | |
mutate(original = paste0(original, " (", default, ")")) %>% | |
select(-default) | |
convert_df <- args %>% | |
dplyr::select(-model) %>% | |
tidyr::pivot_wider(names_from = engine, values_from = original) | |
convert_df %>% | |
knitr::kable(col.names = paste0("**", colnames(convert_df), "**")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment