Caret 中的最小二乘支持向量机失败

Least Square Support Vector Machine in Caret fail

我尝试通过 R 中的 caret 包来拟合最小二乘支持向量机,但我无法让它工作。即使对于像这样极端简单的例子,它也会失败:

library(caret)
library(tidyverse)
data("iris")


#to make this example a binary classification task

iris <- iris %>% filter(Species %in% c("setosa", "versicolor")) %>%
    mutate(Species = droplevels(Species))

svmls <- train(Species ~ .,
               iris,
               method = "lssvmLinear",
               preProc = c("center", "scale")
               )

有几个像这样的警告:

In eval(xpr, envir = envir) :
  model fit failed for Resample09: tau=0.0625 Error in if (truegain[k] < tol) break : 
  missing value where TRUE/FALSE needed

直接从kernlab调用lssmv函数成功:

library(kernlab)
svmls2 <- lssvm(Species~.,data=iris)
svmls2

如果您能猜出问题所在,我将不胜感激。

我知道这个问题已经很老了,但这里有一些答案

我也遇到了同样的错误,当深入查看时,LSSVM Linear 的 Caret Default 正在使用 Polygonal Kernel,看起来像这样:

getModelInfo()$lssvmLinear$fit
function(x, y, wts, param, lev, last, classProbs, ...) {
                    kernlab::lssvm(x = as.matrix(x), y = y,
                                   tau = param$tau,
                                   kernel = kernlab::polydot(degree = 1,
                                                             scale = 1,
                                                             offset = 1), ...)    
                  }

因此我将其编辑为仅使用默认内核,这样它就可以像预期的那样运行:

newlssvm <- getModelInfo()$lssvmLinear
newlssvm$fit <- function(x, y, wts, param, lev, last, classProbs, ...) {
  kernlab::lssvm(x = as.matrix(x), y = y,
                 tau = param$tau)    
}

svmls <- train(Species ~ .,
               iris,
               method = newlssvm,
               preProc = c("center", "scale")
               )

我声明这个问题出在 kernlab,因为:

lssvm(Species~.,data= iris, kernel = kernlab::polydot(degree = 2,
                                                      scale = 0.01, offset=1))

给出类似这样的错误:

Error in if (truegain[k] < tol) break : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In sqrt(G[kadv, kadv]) : NaNs produced