我一直在使用该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:如果需要,我会尽量提供数据进行测试。