我正在尝试使用kernlab
R包来执行支持向量机(SVM)。对于我非常简单的例子,我有两条训练数据。 A和B.
(A和B的类型为matrix
- 它们是图的邻接矩阵。)
所以我写了一个函数,它接受A + B并生成一个核矩阵。
> km
[,1] [,2]
[1,] 14.33333 18.47368
[2,] 18.47368 38.96053
现在我使用kernlab
的{{1}}函数来生成我的预测模型。现在,我只想努力工作 - 我不担心训练错误等。
所以,问题1 :我是否正确生成模型?合理?
ksvm
到目前为止一切顺利。我们创建了自定义内核矩阵,然后使用该矩阵创建了一个ksvm模型。我们的训练数据标记为“1”和“-1”。
现在预测:
# y are my classes. In this case, A is in class "1" and B is in class "-1"
> y
[1] 1 -1
> model2 = ksvm(km, y, type="C-svc", kernel = "matrix");
> model2
Support Vector Machine object of class "ksvm"
SV type: C-svc (classification)
parameter : cost C = 1
[1] " Kernel matrix used as input."
Number of Support Vectors : 2
Objective Function Value : -0.1224
Training error : 0
糟糕,这没关系。有点期待,真的。 “预测”需要某种矢量,而不是矩阵。
让我们尝试一些事情:
> A
[,1] [,2] [,3]
[1,] 0 1 1
[2,] 1 0 1
[3,] 0 0 0
> predict(model2, A)
Error in as.matrix(Z) : object 'Z' not found
上面的一些测试是荒谬的,但这是我的观点:无论我做什么,我都无法获得预测()来查看我的数据并进行预测。标量不起作用,向量不起作用。 2x2矩阵不起作用,3x3矩阵也不起作用。
我在这里做错了什么?
(一旦我弄清楚ksvm 想要什么,那么我可以确保我的测试数据能够以合理/合理/数学合理的方式符合该格式。)
答案 0 :(得分:21)
答案 1 :(得分:2)
首先,我没有多使用kernlab。但只是查看文档,我确实看到了predict.ksvm()
方法的工作示例。复制和粘贴,并省略打印到屏幕:
## example using the promotergene data set
data(promotergene)
## create test and training set
ind <- sample(1:dim(promotergene)[1],20)
genetrain <- promotergene[-ind, ]
genetest <- promotergene[ind, ]
## train a support vector machine
gene <- ksvm(Class~.,data=genetrain,kernel="rbfdot",\
kpar=list(sigma=0.015),C=70,cross=4,prob.model=TRUE)
## predict gene type probabilities on the test set
genetype <- predict(gene,genetest,type="probabilities")
这似乎非常简单:使用随机抽样生成训练集genetrain
及其补集genetest
,然后通过ksvm
拟合并调用predict()
使用fit的方法和匹配格式的新数据。这是非常标准的。
你可能会发现Max Kuhn的caret包很有用。它为各种回归,分类和机器学习方法和包提供了一般评估和测试框架,包括kernlab,并包含几个小插图和JSS paper。
答案 2 :(得分:1)
在kernlab中它有点连线,并且在预测时需要每个测试示例和支持向量之间的输入内核矩阵。你需要自己找到这个矩阵。
例如,测试矩阵[n x m],其中n是测试样本的数量,m是学习模型中的支持向量数(按SVindex(模型)的顺序排序)。
示例代码
trmat <- as.kernelMatrix(kernels[trainidx,trainidx])
tsmat <- as.kernelMatrix(kernels[testidx,trainidx])
#training
model = ksvm(x=trmat, y=trlabels, type = "C-svc", C = 1)
#testing
thistsmat = as.kernelMatrix(tsmat[,SVindex(model)])
tsprediction = predict(model, thistsmat, type = "decision")
kernels是输入内核矩阵。 trainidx和testidx是训练和测试的ids。
答案 3 :(得分:0)
自己根据解决方案的元素构建标签。使用此备用预测器方法,该方法采用ksvm模型(m)和原始训练格式(d)中的数据
predict.alt <- function(m, d){
sign(d[, m@SVindex] %*% m@coef[[1]] - m@b)
}
K是培训的kernelMatrix
。出于验证的考虑,如果您对训练数据运行predict.alt
,您会注意到备用预测器方法会将值与ksvm返回的拟合值一起切换。本机预测器以意想不到的方式运行:
aux <- data.frame(fit=kout@fitted, native=predict(kout, K), alt=predict.alt(m=kout, d=as.matrix(K)))
sample_n(aux, 10)
fit native alt
1 0 0 -1
100 1 0 1
218 1 0 1
200 1 0 1
182 1 0 1
87 0 0 -1
183 1 0 1
174 1 0 1
94 1 0 1
165 1 0 1