建立 workflow
當你的資料已準備好可供分析時,你將宣告一個 logistic_model(),用來預測航班是否會延誤抵達。
你會把 flight 變數指派為「ID」角色,以便在分析與除錯時作為參考。接著從 date 變數建立新特徵,以明確刻劃假期的影響,並將 factors 表示為虛擬變數(dummy variables)。
使用 workflow() 將你的「模型」與 recipe() 一起打包,有助於確保後續的擬合或預測會一致地執行相同的特徵工程步驟。
本練習屬於課程
R 的特徵工程
練習說明
- 將
flight指派為「ID」角色。 - 將模型與 recipe 打包成
workflow物件。 - 將
lr_workflow擬合到test資料。 - 將已擬合的 workflow 整理為 tidy。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
lr_model <- logistic_reg()
# Assign an "ID" role to flight
lr_recipe <- recipe(arrival ~., data = train) %>% update_role(flight, new_role = ___) %>%
step_holiday(date, holidays = timeDate::listHolidays("US")) %>% step_dummy(all_nominal_predictors())
# Bundle the model and the recipe into a workflow object
lr_workflow <- workflow() %>% add_model(___) %>% add_recipe(___)
lr_workflow
# Fit lr_workflow workflow to the test data
lr_fit <- lr_workflow %>% ___(data = test)
# Tidy the fitted workflow
tidy(___)