重新介紹 glmnet

使用 R 的 caret 進行 Machine Learning

Zach Mayer

Data Scientist at DataRobot and co-author of caret

glmnet 複習

  • 具內建變數選擇的線性模型
  • 絕佳的基準模型
  • 優點
    • 擬合速度快
    • 忽略雜訊變數
    • 係數易於解讀
使用 R 的 caret 進行 Machine Learning

範例:在流失資料上使用 glmnet

set.seed(42)
model_glmnet <- train(
  churn ~ ., 
  churnTrain,
  metric = "ROC",
  method = "glmnet",
  tuneGrid = expand.grid(
    alpha = 0:1, 
    lambda = 0:10 / 10
  ),
  trControl = myControl
)
使用 R 的 caret 進行 Machine Learning

視覺化結果

plot(model_glmnet)

此圖為 ROC 對正規化參數的折線圖,混合比例為 0 與 1 兩條曲線;當混合比例為 1 時 ROC 明顯較高。

使用 R 的 caret 進行 Machine Learning

繪製係數圖

plot(model_glmnet$finalModel)

此圖為自訂 glmnet 繪圖:係數相對 L1 範數的關係。

使用 R 的 caret 進行 Machine Learning

一起來練習吧!

使用 R 的 caret 進行 Machine Learning

Preparing Video For Download...