開始使用免費開始

調整 penalty

你已經確信 Lasso 能在維持可接受效能的同時,合理地減少模型的特徵數量。接下來你想透過選擇最佳的懲罰項數值來調整模型。環境中已載入基本的 recipe,以及 traintest 的切分。

本練習屬於課程

R 的特徵工程

檢視課程

練習說明

  • 將模型設定為自動調整懲罰項。
  • 設定一個包含 30 個層級的懲罰項網格。

動手互動練習

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

# Set up your model so that the penalty is tuned automatically
model_lasso_tuned <- logistic_reg() %>% set_engine("glmnet") %>%
  set_args(mixture = 1, ___ = ___) 
workflow_lasso_tuned <- workflow() %>%
  add_model(model_lasso_tuned) %>%
  add_recipe(recipe)

# Configure a penalty grid with 30 levels
penalty_grid <- grid_regular(penalty(range = c(-3, 1)), ___ = ___)

tune_output <- tune_grid(workflow_lasso_tuned,
  resamples = vfold_cv(train, v = 5),
  metrics = metric_set(roc_auc),grid = penalty_grid)

autoplot(tune_output)
編輯並執行程式碼