Skip to content

Instantly share code, notes, and snippets.

@dengemann
Created December 25, 2020 21:52
Show Gist options
  • Save dengemann/a9e43ea9af2cfdf180ab23548bbe541c to your computer and use it in GitHub Desktop.
Save dengemann/a9e43ea9af2cfdf180ab23548bbe541c to your computer and use it in GitHub Desktop.
# License: BSD (3-clause)
# Author: Denis A. Engemann <[email protected]>
library(tidymodels)
library(readr)
library(microbenchmark)
hotels <-
read_csv('https://tidymodels.org/start/case-study/hotels.csv') %>%
mutate_if(is.character, as.factor)
set.seed(123)
splits <- initial_split(hotels, strata = children)
hotel_other <- training(splits)
hotel_test <- testing(splits)
set.seed(234)
val_set <- validation_split(hotel_other,
strata = children,
prop = 0.80)
set.seed(345)
rf_recipe <-
recipe(children ~ ., data = hotel_other) %>%
step_date(arrival_date) %>%
step_holiday(arrival_date) %>%
step_rm(arrival_date)
run_rf <- function(cores){
rf_mod <-
rand_forest(mtry = 8, min_n = 7, trees = 100) %>%
set_engine("ranger", num.threads = cores, importance = "impurity") %>%
set_mode("classification")
rf_workflow <-
workflow() %>%
add_model(rf_mod) %>%
add_recipe(rf_recipe)
rf_workflow %>%
last_fit(splits)
}
rf_bench <- microbenchmark(
cores_1 = run_rf(cores = 1),
cores_2 = run_rf(cores = 2),
cores_3 = run_rf(cores = 3),
cores_4 = run_rf(cores = 4),
cores_5 = run_rf(cores = 5),
cores_6 = run_rf(cores = 6),
cores_7 = run_rf(cores = 7),
cores_8 = run_rf(cores = 8),
times = 10
)
rf_bench$arch <- sessionInfo()[[1]][[2]]
rf_bench$os <- sessionInfo()[[4]]
name <- gsub(" ", "-", date(), fixed = T)
name <- paste0("bench_rf_", name, ".csv")
write_csv(rf_bench, name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment