首页 > 解决方案 > 获取 SVM 中的特征重要性

问题描述

我使用带有线性内核的 SVM 进行了多类 (3) 分类。

对于这个任务,我使用了这个mlr包。SVM 来自kernlab包。

library(mlr)
library(kernlab)

print(filtered_task)

Supervised task: dtm
Type: classif
Target: target_lable
Observations: 1462
Features:
   numerics     factors     ordered functionals 
        291           0           0           0 
Missings: FALSE
Has weights: FALSE
Has blocking: FALSE
Has coordinates: FALSE
Classes: 3
negative  neutral positive 
     917      309      236 
Positive class: NA

lrn = makeLearner("classif.ksvm", par.vals = list(kernel = "vanilladot"))
mod = mlr::train(lrn, train_task)

现在我想知道每个类中哪些特征的权重最高。知道如何到达那里吗?

此外,最好为交叉验证结果获得每个类的特征权重。

rdesc = makeResampleDesc("CV",
                         iters = 10,
                         stratify = T) 
set.seed(3)
r = resample(lrn, filtered_task, rdesc)

我知道有可能像下面这样计算特征重要性,这与蒙特卡洛迭代的交叉验证结果相似。

imp = generateFeatureImportanceData(task = train_task, 
                                    method = "permutation.importance", 
                                    learner = lrn,
                                    nmc = 10)

但是,对于这种方法,我无法获得每个类的特征重要性,而只能获得整体重要性。

library(dplyr)
library(ggplot)

imp_data = melt(imp$res[, 2:ncol(imp$res)]) 

imp_data = imp_data %>% 
  arrange(-value)

imp_data[1:10,] %>% 
  ggplot(aes(x = reorder(variable, value), y = value)) + 
  geom_bar(stat = "identity",  fill = "darkred") + 
  labs(x = "Features", y = "Permutation Importance") +
  coord_flip() +
  theme_minimal()

在此处输入图像描述

标签: rsvmmlr

解决方案


推荐阅读