2016-05-18 46 views
1

我一直在使用kernlab软件包,并且使用ksvm/predict函数和预计算内核来解决问题。使用带预计算内核的kernlab软件包时出错

我已经得到该错误消息:

> 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 

然后,我看了看co根据模型存储的列,发现如下:

> 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:我会尝试提供测试数据,如果需要的话。

回答

0

你这样做是对的。预测对象只需要新数据和支持向量之间的核心距离,但它本身不提取它们,您必须自己传递它们。

试试这个:

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.outerkernelMatrix测量测试数据(行)和列车数据(列)之间的内核距离。