Recap of machine learning basics

Hyperparameter Tuning in R

Dr. Shirin Elsinghorst

Senior Data Scientist

Machine learning with caret - splitting data

# Load caret and set seed
library(caret)
set.seed(42)

# Create partition index
index <- createDataPartition(breast_cancer_data$diagnosis, p = .70, 
                             list = FALSE)

# Subset `breast_cancer_data` with index bc_train_data <- breast_cancer_data[index, ] bc_test_data <- breast_cancer_data[-index, ]
  • Training set with enough power.
  • Representative test set.
Hyperparameter Tuning in R

Train a machine learning model with caret

  • Set up cross-validation:
library(caret)
library(tictoc)
fitControl <- trainControl(method = "repeatedcv", number = 3, repeats = 5)
  • Train a Random Forest model:
tic()
set.seed(42)
rf_model <- train(diagnosis ~ ., data = bc_train_data, method = "rf", trControl = fitControl,
                  verbose = FALSE)
toc()
1.431 sec elapsed
Hyperparameter Tuning in R

Automatic hyperparameter tuning in caret

Random Forest 

...

Resampling results across tuning parameters:

  mtry  Accuracy   Kappa    
   2    0.9006783  0.8015924
   6    0.9126645  0.8253289
  10    0.8999389  0.7999386

Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mtry = 6.
Hyperparameter Tuning in R

Let's start modeling!

Hyperparameter Tuning in R

Preparing Video For Download...