在惩罚逻辑回归模型中提取特征重要性

extract feature importance in penalized logistc regression model

我将 tidymodels 网站上的这个示例用于我自己的数据 (https://www.tidymodels.org/start/case-study/)。 与此示例相反,我的数据表明,惩罚逻辑回归在准确性方面优于随机森林。 然而,在这个例子中,它没有描述如何从惩罚性逻辑回归 (GLMNET) 模型中评估特征重要性。 我的问题是这个模型是否选择了一些预测变量进入模型?如果是,您如何确定选择了哪些特征以及如何从惩罚性物流回归 (glmnet) 中找出特征的重要性? 非常感谢您的回答

如果您真的按照 link 中的示例进行操作,您会注意到它们设置了 mixture=1 ,这基本上是 运行 一个 lasso from glmnet. You should most likely try to tune your penalty term, but in the end, the coefficients which are non zero are the ones selected. You can read this help page on glmnet,我认为它们涵盖了还不错。

使用一个非常小的例子,我将 penaltylambda 设置为 0.01,您可以看到哪些系数是非零的:

library(tidymodels)
library(mlbench) 

data(Sonar)

lr_mod <- 
  logistic_reg(penalty = 0.01, mixture = 1) %>% 
  set_engine("glmnet") %>%
  fit(Class ~. , data = Sonar)

reg_coef = coef(lr_mod$fit,s=0.01)

这些被选中(包括拦截):

reg_coef[reg_coef[,1]>0,]
 (Intercept)           V3           V7           V8          V14          V16 
 4.515343499  7.554970397  4.682233799  3.056506356  0.003707144  2.506047798 
         V31          V36          V37          V40          V50          V55 
 3.069514694  2.271717076  1.270513186  1.697256135 32.854319954 12.996429503 
         V57 
36.520376537 

这些被踢出:

reg_coef[reg_coef[,1]==0,]
 V2  V5  V6 V10 V13 V15 V17 V18 V19 V25 V26 V27 V33 V34 V35 V38 V41 V42 V43 V46 
  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 
V47 V53 V56 V60 
  0   0   0   0