2016-05-18 45 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測量測試數據(行)和列車數據(列)之間的內核距離。