(Caret)包中机器学习模型的特征重要性

Feature Importance for machine learning models in (Caret)package

我对 Caret 包中的特征重要性函数有疑问。

我有一个具有更多数值和因子特征的数据集。 我使用下面的命令来获取模型的特征重要性。它给出了每个 (sub_feature) 对于因子变量的重要性。但是,我只想了解功能本身的重要性,而无需详细了解功能的每个因素。

gbmImp <- caret::varImp(xgb1, scale = TRUE)

我将创建一些示例数据,因为我们没有您问题中的任何数据:

library(caret)

# example data
df <- data.frame("x" = rnorm(100),
                 "fac" = as.factor(sample(c(rep("A", 30), rep("B", 35), rep("C", 35)))),
                 "y" = as.numeric((rpois(100, 4))))
# model
model <- train(y ~ ., method = "glm", data = df)
# feature importance
varImp(model, scale = TRUE)

这个 returns 您不希望出现在问题中的特征重要性:

# glm variable importance
#
#      Overall
# facB  100.00
# facC   13.08
# x       0.00

您可以将因子变量转换为数字并执行相同的操作:

# make the factor variable numeric
trans_df <- transform(df, fac = as.numeric(fac))
# model
trans_model <- train(y ~ ., method = "glm", data = trans_df)
# feature importance
varImp(trans_model, scale = TRUE)

returns 'overall' 特征的重要性:

# glm variable importance
# 
#     Overall
# x       100
# fac       0

但是,当我们 运行 varImp(trans_model, scale = TRUE).

时,我不知道对因子变量的 as.numeric() 操作是否不会导致不同的特征重要性

此外,如果您发现特定的 factor/character 变量在转换为数字时有问题,请查看 this SO thread