一次比较所有 ksvm 内核的性能

comparing performance of all ksvm kernels at once

我是 R 的新手,所以这可能是个愚蠢的问题,但我正在寻找一种方法来遍历 kernlab 中 ksvm 函数中所有可能的内核选项并输出 table结果。

现在我有一个基本设置:

# call ksvm
model <-  ksvm(as.matrix(data[,1:10]),as.factor(data[,11]),type="C-svc",kernel="vanilladot",C=100,scaled=TRUE)
# calculate a1.am
a <- colSums(model@xmatrix[[1]] * model@coef[[1]])
a
# calculate a0
a0 <- -model@b
a0
# see what the model predicts
pred <- predict(model,data[,1:10])
pred
# see what fraction of the model's predictions match the actual classification
sum(pred == data[,11]) / nrow(data)

它吐出所有预测和准确度指标

[1] 0.8639144

理想情况下,我想要的是 table 看起来像这样

kernel       accuracy
vanilladot   0.8639144
polydot      0.7285432
besseldot    1
...          ...

是否有快速简便的方法,或者是手动创建带有模型名称和精度指标的 table 然后打印或绘制的唯一方法?

您可以在 for 循环中迭代所有内核:

myKernels = c("vanilladot","polydot","besseldot")
results=list()
for(i in 1:length(myKernels)){
  # call ksvm using  kernel instead of linear
  model <-  ksvm(as.matrix(data[,1:10]),as.factor(data[,11]),type="C-svc",kernel=myKernels[[i]],C=100,scaled=TRUE)
  # calculate a1.am
  a <- colSums(model@xmatrix[[1]] * model@coef[[1]])
  a
  # calculate a0
  a0 <- -model@b
  a0
  # see what the model predicts
  pred <- predict(model,data[,1:10])
  pred
  # see what fraction of the model's predictions match the actual classification
  results[[i]]=data.table(kernel=myKernels[[i]],accuracy=sum(pred == data[,11]) / nrow(data))
}
rindlist(results)