建立 recipe-model workflow
tidymodels 套件可以把 recipe 與 model 組合成 workflow。Workflow 讓你容易建立一條處理資料與訓練模型的步驟管線。之後也能輕鬆把同一個 workflow 套用到新資料,而不需要重新定義所有前處理與建模步驟。方便的是,workflow 提供 fit() 函式,能同時把 recipe 與 model 配適到資料。
在這個練習中,你會練習建立一個 recipe 與一個 model,並把它們加入到 workflow,讓它們可以直接對資料進行配適。員工健保離職資料的 train 與 test 資料集已可使用。目標變數是 Attrition。
已為你載入 tidyverse 與 tidymodels 套件。
本練習屬於課程
R 的降維
練習說明
- 使用
train資料定義一個 recipe,依序加入step_filter_missing()、step_scale()、與step_nzv(),分別用來移除 NA、標準化數值特徵、以及移除低變異特徵。step_filter_missing()的門檻請設為 0.5。 - 使用「glm」引擎定義一個羅吉斯迴歸模型。
- 將
feature_selection_recipe與lr_model加入到名為attrition_wflow的 workflow。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create recipe
feature_selection_recipe <-
___(___ ~ ., data = ___) %>%
___(___(), threshold = 0.5) %>%
___(___()) %>%
___(___()) %>%
prep()
# Create model
lr_model <- ___() %>%
___("___")
# Add recipe and model to a workflow
attrition_wflow <- ___() %>%
___(___) %>%
___(___)