重用 trainControl

使用 R 的 caret 進行 Machine Learning

Max Kuhn

Software Engineer at RStudio and creator of caret

真實情境範例

  • 資料:電信公司的客戶流失
  • 訓練多種模型並選出最佳者
  • 各模型必須使用相同的訓練/測試分割
  • 建立共用的 trainControl 物件
使用 R 的 caret 進行 Machine Learning

範例:客戶流失資料

# Summarize the target variables
library(caret)
library(C50)
data(churn)
table(churnTrain$churn) / nrow(churnTrain)
      yes        no 
0.1449145 0.8550855 
使用 R 的 caret 進行 Machine Learning

範例:客戶流失資料

# 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 進行 Machine Learning

範例:客戶流失資料

myControl <- trainControl(
  summaryFunction = twoClassSummary,
  classProbs = TRUE,
  verboseIter = TRUE,
  savePredictions = TRUE,
  index = myFolds
)
  • 使用折疊建立 trainControl 物件
  • 各模型使用完全相同的交叉驗證折疊
使用 R 的 caret 進行 Machine Learning

一起來練習吧!

使用 R 的 caret 進行 Machine Learning

Preparing Video For Download...