1

我试图在我的测试数据集 test_k 中获得每个观察的概率。我正在使用预先计算的内核矩阵和 C 分类。“C-svc”应该让我得到一个概率模型。但是,我收到以下错误代码:错误:$ 运算符对原子向量无效。为什么?为什么源代码需要 oldco != newncols?源代码中的对象不是我的ksvm.model吗?

  ksvm.model
  # the kernel parameter is set to "matrix", calling the kernel matrix interface.
 
  test_k <- as.kernelMatrix(k_mat[(train_row + 1):(train_row + test_row), SVindex(ksvm.model), drop = F])
  # SVindex(ksvm.model) returns the indexes of the support vectors. This ensures that test_k only has the rows of the test data and the columns that correspond to the support vectors.

predict(ksvm.model, test_k, type='probabilities') 

### This is the error
Error: $ operator is invalid for atomic vectors 


### This is the source code of kernlab 
##**************************************************************#
## predict for matrix, data.frame input

setMethod("predict", signature(object = "ksvm"),
function (object, newdata, type = "response", coupler = "minpair")
{
  type <- match.arg(type,c("response","probabilities","votes","decision"))
  if (missing(newdata) && type=="response" & !is.null(fitted(object)))
    return(fitted(object))
  else if(missing(newdata))
    stop("Missing data !")

 if(!is(newdata,"list")){ 
  if (!is.null(terms(object)) & !is(newdata,"kernelMatrix"))
    {
      if(!is.matrix(newdata))
        newdata <- model.matrix(delete.response(terms(object)), as.data.frame(newdata), na.action = n.action(object))
    }
  else
    newdata  <- if (is.vector(newdata)) t(t(newdata)) else as.matrix(newdata)

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 !")
`
<https://github.com/cran/kernlab/blob/master/R/ksvm.R> ```

 

它在这里工作?他在那里做什么 iris[c(3, 10, 56, 68,107, 120), -5]?,为什么需要这个?

irismodel <- ksvm(Species ~ ., data = iris,type = "C-bsvc", kernel = "rbfdot",kpar = list(sigma = 0.1), C = 10, prob.model = TRUE) 

predict(irismodel, iris[c(3, 10, 56, 68,107, 120), -5], type = "probabilities")
 <https://www.researchgate.net/publication/5142924_Support_Vector_Machines_in_R>
4

0 回答 0