我一直在使用kernlab
包,在使用带有预计算内核的ksvm
/predict
函数时遇到一些问题。
我得到的错误消息是:
> ksvm.mod <- ksvm(trainingset.outer, traininglabels.outer, kernel = "matrix",type="C-svc", C = 60, prob.model = TRUE)
> temp <- predict(ksvm.mod, test.kernel.outer)
Error in .local(object, ...) : test vector does not match model !
我查看了错误位置的源代码,发现这是由于列的差异造成的
newnrows <- nrow(newdata)
newncols <- ncol(newdata)
if(!is(newdata,"kernelMatrix") && !is.null(xmatrix(object))){
if(is(xmatrix(object),"list") && is(xmatrix(object)[[1]],"matrix")) oldco <- ncol(xmatrix(object)[[1]])
if(is(xmatrix(object),"matrix")) oldco <- ncol(xmatrix(object))
if (oldco != newncols) stop ("test vector does not match model !")
}
但是,我使用的对象具有相等的列
> ncol(trainingset.outer)
[1] 1498
> ncol(test.kernel.outer)
[1] 1498
然后,我查看了根据模型存储的列,并发现了以下内容:
> ncol(xmatrix(ksvm.mod)[[1]])
Error in xmatrix(ksvm.mod)[[1]] : subscript out of bounds
> xmatrix(ksvm.mod)[[1]]
Error in xmatrix(ksvm.mod)[[1]] : subscript out of bounds
> xmatrix(ksvm.mod)
<0 x 0 matrix>
> ?xmatrix
> ksvm.mod
Support Vector Machine object of class "ksvm"
SV type: C-svc (classification)
parameter : cost C = 60
[1] " Kernel matrix used as input."
Number of Support Vectors : 831
Objective Function Value : -211534.1
Training error : 0.257677
Probability model included.
> ncol(xmatrix(gene)[[1]]) # for dataframes used without precomputed kernels
[1] 172
我猜模型没有存储任何对象,我的理解正确吗?由于web上没有很好的例子来使用带有预计算内核的包,所以我写这篇文章是为了寻求你的帮助。
PS:如果需要,我会尝试提供测试数据。
发布于 2016-06-29 00:07:22
你做的只对了一半。predict对象只需要newdata和支持向量之间的内核距离,但它不会自己提取它们,您必须自己传递它们。
试试这个:
ksvm.mod <- ksvm(trainingset.outer, traininglabels.outer, kernel = "matrix",type="C-svc", C = 60, prob.model = TRUE)
temp <- predict(ksvm.mod, test.kernel.outer[, SVindex(ksvm.mod))
我在这里假设test.kernel.outer
是一个kernelMatrix
,它测量测试数据(行)和训练数据(列)之间的内核距离。
https://stackoverflow.com/questions/37290816
复制相似问题