依變異數選擇特徵

R 的降維

Matt Pickard

Owner, Pickard Predictives, LLC

未標準化資料的變異數

未標準化信用資料的平均與標準差誤差棒圖

R 的降維

標準化資料的變異數

標準化信用資料的平均與標準差誤差棒圖

R 的降維

計算標準化後的變異數

credit_variances <- credit_df %>% 
  summarize(across(everything(), ~ var(scale(., center = FALSE)), na.rm = TRUE)) %>%

pivot_longer(everything(), names_to = "feature", values_to = "variance") %>%
arrange(desc(variance)) credit_variances
# A tibble: 17 × 2
   feature                  variance
   <chr>                       <dbl>
 1 num_of_loan               0.996  
 2 num_of_delayed_payment    0.986   
 ...
R 的降維

變異數門檻

變異數輸出

R 的降維

變異數門檻

變異數輸出,顯示首個可能門檻

R 的降維

變異數門檻

變異數輸出,顯示首個可能門檻

R 的降維

變異數門檻

變異數輸出,顯示首個可能門檻

R 的降維

變異數門檻圖

變異數門檻圖

R 的降維

建立變異數篩選器

low_var_filter <- credit_variances %>% 
  filter(variance < 0.1) %>% 
  pull(feature)

low_var_filter
[1] "credit_history_months"    "age"                     
[3] "num_credit_inquiries"     "credit_utilization_ratio"
[5] "num_credit_card"   
R 的降維

tidymodels 做法

建立 recipe

low_variance_recipe <- recipe(credit_score ~ ., data = credit_df) %>%

step_zv(all_predictors()) %>%
step_scale(all_numeric_predictors()) %>%
step_nzv(all_predictors()) %>%
prep()

套用 recipe

filtered_credit_df <- bake(low_variance_recipe, new_data = NULL)
R 的降維

檢視特定步驟的影響

low_variance_recipe <- recipe(credit_score ~ ., data = credit_df) %>% 
  step_zv(all_predictors()) %>% 
  step_scale(all_numeric_predictors()) %>% 
  step_nzv(all_predictors()) %>% 
  prep() 

tidy(low_variance_recipe, number = 3)
  terms                id       
  <chr>                <chr>    
1 num_credit_card      nzv_ni8L7
2 num_credit_inquiries nzv_ni8L7
R 的降維

一起來練習吧!

R 的降維

Preparing Video For Download...