r - 获取 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()
解决方案
推荐阅读
- vscode-settings - 用空白板打开 VSCode
- python - 从文本文件导入数字和数据并将它们单独添加到数组中并在数组中输入位置以获取值
- c# - 如何在列表中存储多个泛型类型
>? - mysql - 具有功能的 MySQL CASE 构造函数
- vb.net - AVL树如何在插入时平衡树
- c# - 使用 timeScale 0f 暂停游戏时如何暂停/恢复协程?
- git - 如何更改提交编辑器中显示的内容?
- c++ - 是否有一种解决方法可以在 c++ 中为短裤定义用户定义的文字?
- bash - 比较 2 个 csv 文件并将常用字段记录合并到第一个文件中
- javascript - Vue 组件通过上下文获取 props