我是R的新手,所以这可能是一个愚蠢的问题,但我正在寻找一种方法来迭代kernlab中ksvm函数中所有可能的内核选项并吐出一个结果表。
现在我有一个基本的设置:
# 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
理想情况下,我想要的是一个看起来像这样的表
kernel accuracy
vanilladot 0.8639144
polydot 0.7285432
besseldot 1
... ...
是否有快速简便的方法,或者是手动创建具有型号名称和精度指标的表格然后打印或绘制它的唯一方法?
答案 0 :(得分:1)
您可以在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)