-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hyperparameter Tuning.Rmd
82 lines (53 loc) · 1.52 KB
/
Hyperparameter Tuning.Rmd
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
---
title: "Hyperparameter Tuning"
author: "Ng Wei Keat, 20793486"
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(cache = TRUE, warning = FALSE, message = FALSE, echo = TRUE, dpi = 180, fig.width = 8, fig.height = 5)
library(tidyverse)
```
## Explore data
```{r}
library(tidyverse)
food_consumption <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-18/food_consumption.csv')
```
```{r}
library(countrycode)
food <- food_consumption %>%
mutate(continent = countrycode(country,
origin = "country.name",
destination = "continent")) %>%
select(-co2_emmission) %>%
pivot_wider(names_from = food_category, values_from = consumption) %>%
janitor::clean_names() %>%
mutate(asia = case_when(continent == "Asia" ~ "Asia", TRUE ~ "Other")) %>%
select(-country, -continent) %>%
mutate_if(is.character, factor)
```
```{r}
library(GGally)
ggscatmat(food, columns = 1:11, color = "asia", alpha = 0.6)
```
## Tune hyperparameters
```{r}
library(tidymodels)
set.seed(20793486)
food_boot <- bootstraps(food, times = 30)
rf_spec <- rand_forest(mode = "classification",
mtr = tune(), trees = 1000, min_n = tune()) %>%
set_engine("ranger")
rf_grid <- tune_grid(
asia ~.,
model = rf_spec,
resamples = food_boot
)
```
```{r}
rf_grid %>%
collect_metrics()
rf_grid %>%
show_best("roc_auc")
```
Can use parallel processing to speed this up!!