Hyperparameter tuning with caret

Hyperparameter Tuning in R

Dr. Shirin Elsinghorst

Senior Data Scientist

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

Hyperparameters are specific to model algorithms

Hyperparameter Tuning in R

Hyperparameters in Support Vector Machines (SVM)

fitControl <- trainControl(method = "repeatedcv", number = 3, repeats = 5)

tic()
svm_model <- train(diagnosis ~ ., 
                   data = bc_train_data, 
                   method = "svmPoly", 
                   trControl = fitControl,
                   verbose= FALSE)
toc()
3.836 sec elapsed
Hyperparameter Tuning in R

Hyperparameters in Support Vector Machines (SVM)

svm_model
Support Vector Machines with Polynomial Kernel 
... 

Resampling results across tuning parameters:

  degree  scale  C     Accuracy   Kappa    
  1       0.100  1.00  0.9104803  0.8211459

Accuracy was used to select the optimal model using the largest value.
The final values used for the model were degree = 1, scale = 0.1 and C = 1.
Hyperparameter Tuning in R

Defining hyperparameters for automatic tuning

  • tuneLength
tic()
set.seed(42)
svm_model_2 <- train(diagnosis ~ ., 
                     data = bc_train_data, 
                     method = "svmPoly", 
                     trControl = fitControl,
                     verbose = FALSE,
                     tuneLength = 5)
toc()
7.458 sec elapsed

Accuracy was used to select the optimal model using the largest value.
The final values used for the model were degree = 1, scale = 1 and C = 1.
Hyperparameter Tuning in R

Manual hyperparameter tuning in caret

  • tuneGrid + expand.grid
hyperparams <- expand.grid(degree = 4, scale = 1, C = 1)

tic() set.seed(42) svm_model_3 <- train(diagnosis ~ ., data = bc_train_data, method = "svmPoly", trControl = fitControl, tuneGrid = hyperparams, verbose = FALSE) toc()
0.691 sec elapsed
Hyperparameter Tuning in R

Manual hyperparameter tuning in caret

svm_model_3
Support Vector Machines with Polynomial Kernel 

...

  Accuracy   Kappa   
  0.7772947  0.554812

Tuning parameter 'degree' was held constant at a value of 4
Tuning parameter 'scale' was held constant at a value of 1
Tuning parameter 'C' was
 held constant at a value of 1
Hyperparameter Tuning in R

It's your turn!

Hyperparameter Tuning in R

Preparing Video For Download...