使用 caret 调参

R 中的超参数调优

Dr. Shirin Elsinghorst

Senior Data Scientist

在 caret 中自动调参

随机森林 

...

在不同调参下的重采样结果:

  mtry  准确率     Kappa    
   2    0.9006783  0.8015924
   6    0.9126645  0.8253289
  10    0.8999389  0.7999386

使用最大值的准确率选择最优模型。
最终模型使用的参数为 mtry = 6。
R 中的超参数调优

超参数取决于具体算法

R 中的超参数调优

支持向量机(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 秒
R 中的超参数调优

支持向量机(SVM)的超参数

svm_model
带多项式核的支持向量机 
... 

在不同调参下的重采样结果:

  degree  scale  C     准确率     Kappa    
  1       0.100  1.00  0.9104803  0.8211459

使用最大值的准确率选择最优模型。
最终模型的参数为 degree = 1、scale = 0.1、C = 1。
R 中的超参数调优

为自动调参设置超参数

  • 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 秒

使用最大值的准确率选择最优模型。
最终模型的参数为 degree = 1、scale = 1、C = 1。
R 中的超参数调优

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

在 caret 中手动调参

svm_model_3
带多项式核的支持向量机 

...

  准确率   Kappa   
  0.7772947  0.554812

调参项 'degree' 固定为 4
调参项 'scale' 固定为 1
调参项 'C' 被
 固定为 1
R 中的超参数调优

轮到您了!

R 中的超参数调优

Preparing Video For Download...