机器学习基础回顾

R 中的超参数调优

Dr. Shirin Elsinghorst

Senior Data Scientist

使用 caret 的机器学习——数据切分

# 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, ]
  • 训练集需有足够的统计功效。
  • 测试集需具代表性。
R 中的超参数调优

用 caret 训练机器学习模型

  • 设置交叉验证:
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
R 中的超参数调优

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.
R 中的超参数调优

开始建模!

R 中的超参数调优

Preparing Video For Download...