Hyperparameter Tuning in R
Dr. Shirin Elsinghorst
Senior Data Scientist
# 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, ]
library(caret)
library(tictoc)
fitControl <- trainControl(method = "repeatedcv", number = 3, repeats = 5)
tic()
set.seed(42)
rf_model <- train(diagnosis ~ ., data = bc_train_data, method = "rf", trControl = fitControl,
verbose = FALSE)
toc()
1.431 sec elapsed
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