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

时间:2017-09-13 08:27:11

标签: r machine-learning svm r-caret

我尝试在caret中使用R包来安装最小二乘支持向量机,但我无法使其工作。即使是像这样的极端简单的例子也失败了:

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

我真的很感激任何可能出错的猜测。

1 个答案:

答案 0 :(得分:0)

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

我也遇到了同样的错误,当其中的“ Look depth”(查找深度)为LSSVM Linear的“插入符默认值”使用多边形内核时,如下所示:

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