处理低信息量的预测变量

在 R 中使用 caret 的机器学习

Zach Mayer

Data Scientist at DataRobot and co-author of caret

无(或低)方差变量

  • 有些变量信息量很低
    • 常量(即无方差)
    • 近乎常量(即方差很小)
  • 交叉验证的某一折可能出现常量列
    • 会导致模型出问题
  • 通常会删除极低方差的变量
在 R 中使用 caret 的机器学习

示例:mtcars 中的常量列

# Reproduce dataset from last video
data(mtcars)
set.seed(42)
mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
Y <- mtcars$mpg
X <- mtcars[, 2:4]
# Add constant-valued column to mtcars
X$bad <- 1
在 R 中使用 caret 的机器学习

示例:mtcars 中的常量列

# Try to fit a model with PCA + glm
model <- train(
  X, Y, method = "glm", 
  preProcess = c("center", "scale", "medianImpute", "pca"))
Warning in preProcess.default(thresh = 0.95, k = 5, method = c("medianImpute",  :
  These variables have zero variances: bad
Something is wrong; all the RMSE metric values are missing:
      RMSE        Rsquared  
 Min.   : NA   Min.   : NA  
 1st Qu.: NA   1st Qu.: NA  
 Median : NA   Median : NA  
 Mean   :NaN   Mean   :NaN  
 3rd Qu.: NA   3rd Qu.: NA  
 Max.   : NA   Max.   : NA  
 NA's   :1     NA's   :1   
在 R 中使用 caret 的机器学习

caret 再次出手相助

  • "zv" 移除常量列
  • "nzv" 移除近乎常量列
# 让 caret 在建模时移除这些列
set.seed(42)
model <- train(
  X, Y, method = "glm", 
  preProcess = c("zv", "center", "scale", "medianImpute", "pca")
)
min(model$results$RMSE)
3.402557
在 R 中使用 caret 的机器学习

让我们练习!

在 R 中使用 caret 的机器学习

Preparing Video For Download...