多种预处理方法

在 R 中使用 caret 的机器学习

Zach Mayer

Data Scientist at DataRobot and co-author of caret

preProcess 的广阔天地

  • 远不止中位数或 KNN 缺失填补!
  • 可将多步预处理串联
  • 线性模型常见"配方"(顺序很重要)
    • 中位数填补 ⇒ 中心化 ⇒ 标准化 ⇒ 拟合 glm
  • 详见 ?preProcess
在 R 中使用 caret 的机器学习

示例:预处理 mtcars

# 生成含缺失的数据
data(mtcars)
set.seed(42)
mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
Y <- mtcars$mpg
X <- mtcars[,2:4] # <- 随机缺失
# 使用线性模型"配方"
set.seed(42)
model <- train(
  X, Y, method = "glm",
  preProcess = c("center", "scale", "medianImpute")
)
print(min(model$results$RMSE))
3.612713
在 R 中使用 caret 的机器学习

示例:预处理 mtcars

# 建模前做 PCA
set.seed(42)
model <- train(
  X, Y, method = "glm",
  preProcess = c("center", "scale", "medianImpute", "pca")
)
min(model$results$RMSE)
3.402557
在 R 中使用 caret 的机器学习

示例:预处理 mtcars

# 空间符号变换
set.seed(42)
model <- train(
  X, Y, method = "glm",
  preProcess = c("center", "scale", "medianImpute", "spatialSign")
)
min(model$results$RMSE)
4.284904
在 R 中使用 caret 的机器学习

预处理速查表

  • 先用中位数填补
  • 若非随机缺失,可试 KNN 填补
  • 对线性模型…
    • 先中心化与标准化
    • 可试 PCA 与空间符号变换
  • 树模型通常几乎不需预处理
在 R 中使用 caret 的机器学习

Passons à la pratique !

在 R 中使用 caret 的机器学习

Preparing Video For Download...