复用 trainControl

在 R 中使用 caret 的机器学习

Max Kuhn

Software Engineer at RStudio and creator of caret

真实案例

  • 数据:电信公司客户流失
  • 训练多种模型并选最佳
  • 模型需使用相同的训练/测试划分
  • 创建共享的 trainControl 对象
在 R 中使用 caret 的机器学习

示例:客户流失数据

# Summarize the target variables
library(caret)
library(C50)
data(churn)
table(churnTrain$churn) / nrow(churnTrain)
      yes        no 
0.1449145 0.8550855 
在 R 中使用 caret 的机器学习

示例:客户流失数据

# Create train/test indexes
set.seed(42)
myFolds <- createFolds(churnTrain$churn, k = 5)
# Compare class distribution
i <- myFolds$Fold1
table(churnTrain$churn[i]) / length(i)
      yes        no 
0.1441441 0.8558559
在 R 中使用 caret 的机器学习

示例:客户流失数据

myControl <- trainControl(
  summaryFunction = twoClassSummary,
  classProbs = TRUE,
  verboseIter = TRUE,
  savePredictions = TRUE,
  index = myFolds
)
  • 使用折叠创建 trainControl 对象
  • 各模型使用完全相同的交叉验证折叠
在 R 中使用 caret 的机器学习

Passons à la pratique !

在 R 中使用 caret 的机器学习

Preparing Video For Download...