開始使用免費開始

探索 lasso 迴歸的懲罰值

在上一個練習中,你已完成縮放目標變數與預測變數的所有程式碼。你將使用 train 資料與 lasso_recipe 來建立一個 workflow,以訓練 lasso 迴歸模型,並探索不同懲罰值的影響。當你調整懲罰值並重新訓練模型時,請留意模型中剩下的非零變數數量。你將觀察 lasso 迴歸如何進行特徵選擇。

已為你載入 tidyversetidymodels 套件。

本練習屬於課程

R 的降維

檢視課程

練習說明

  • 以懲罰值 0.001 訓練一個 lasso 迴歸 workflow,並顯示大於 0 的模型係數。
  • 以懲罰值 0.01 重新訓練一個 lasso 迴歸 workflow,並顯示大於 0 的模型係數。
  • 以懲罰值 0.1 重新訓練一個 lasso 迴歸 workflow,並顯示大於 0 的模型係數。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Train workflow model with penalty = 0.001 and view model variables
lasso_model <- linear_reg(___ = ___, mixture = 1, engine = "___")
lasso_workflow <- workflow(preprocessor = lasso_recipe, spec = ___)
tidy(lasso_workflow %>% fit(train)) %>% filter(___ > ___)

# Train the workflow model with penalty = 0.01 and view model variables
lasso_model <- ___(___ = ___, ___ = ___, ___ = "___")
lasso_workflow <- workflow(preprocessor = ___, spec = ___)
tidy(lasso_workflow %>% fit(train)) %>% filter(___ > ___)

# Train the workflow model with penalty = 0.1 and view model variables
lasso_model <- ___(___ = ___, ___ = ___, ___ = "___")
lasso_workflow <- ___ %>% ___(___)
tidy(___ %>% ___(___)) %>% ___(___ > ___)
編輯並執行程式碼