用于在插入符号中进行调整的自定义 SVM 模型出错 [英] Error with custom SVM model for tuning in caret

查看:22
本文介绍了用于在插入符号中进行调整的自定义 SVM 模型出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 此链接 创建自定义 SVM 和通过一些交叉验证运行它.我这样做的主要原因是在我的网格搜索中运行 Sigma、Cost 和 Epsilon 参数,而最近的插入符号模型 (svmRadial) 只能执行其中两个.

I'm trying to follow this link to create a custom SVM and run it through some cross-validations. My primary reason for this is to run Sigma, Cost and Epsilon parameters in my grid-search and the closest caret model (svmRadial) can only do two of those.

当我尝试运行下面的代码时,每次网格迭代都会出现以下错误:

When I attempt to run the code below, I get the following error all over the place at every iteration of my grid:

Warning in eval(expr, envir, enclos) :
  model fit failed for Fold1.: sigma=0.2, C=2, epsilon=0.1 Error in if (!isS4(modelFit) & !(method$label %in% c("Ensemble Partial Least Squares Regression",  : 
  argument is of length zero

即使我逐字复制链接中的代码,我也会遇到类似的错误,我不知道如何解决.我找到了 这个链接自定义模型已构建,我看到引用此错误的位置,但仍不确定问题是什么.我的代码如下:

Even when I replicate the code from the link verbatim, I get a similar error and I'm not sure how to solve it. I found this link which goes through how the custom models are built and I see where this error is referenced, but still not sure what the issue is. I have my code below:

#Generate Tuning Criteria across Parameters
C <- c(1,2)
sigma <- c(0.1,.2)
epsilon <- c(0.1,.2)
grid <- data.frame(C,sigma)

#Parameters
prm <- data.frame(parameter = c("C", "sigma","epsilon"),
                  class = rep("numeric", 3),
                  label = c("Cost", "Sigma", "Epsilon"))

#Tuning Grid
svmGrid <- function(x, y, len = NULL) {
    expand.grid(sigma = sigma,
                C = C,
                epsilon = epsilon)
}

#Fit Element Function
svmFit <- function(x, y, wts, param, lev, last, weights, classProbs, ...) {
    ksvm(x = as.matrix(x), y = y,
         type = "eps-svr",
         kernel = rbfdot,
         kpar = list(sigma = param$sigma),
         C = param$C,
         epsilon = param$epsilon,
         prob.model = classProbs,
         ...)
}

#Predict Element Function
svmPred <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
    predict(modelFit, newdata)

#Sort Element Function
svmSort <- function(x) x[order(x$C),]

#Model
newSVM <- list(type="Regression",
               library="kernlab",
               loop = NULL,
               parameters = prm,
               grid = svmGrid,
               fit = svmFit,
               predict = svmPred,
               prob = NULL,
               sort = svmSort,
               levels = NULL)                    


#Train 
tc<-trainControl("repeatedcv",number=2, repeats = 0,
                 verboseIter = T,savePredictions=T)
svmCV <- train(
    Y~ 1
    + X1
    + X2
    ,data = data_nn,
    method=newSVM, 
    trControl=tc
    ,preProc = c("center","scale"))                 
svmCV

推荐答案

查看第二个链接 提供,我决定尝试在模型的参数中包含一个标签并解决了问题!有趣的是它有效,因为插入符号文档说该值是可选的,但如果它有效,我不能抱怨.

After viewing the second link provided, I decided to try and include a label into the Model's parameters and that solved the issue! It's funny that it worked because the caret documentation says that value is optional, but if it works I can't complain.

#Model 
newSVM <- list(label="My Model",
                   type="Regression",
                   library="kernlab",
                   loop = NULL,
                   parameters = prm,
                   grid = svmGrid,
                   fit = svmFit,
                   predict = svmPred,
                   prob = NULL,
                   sort = svmSort,
                   levels = NULL)

这篇关于用于在插入符号中进行调整的自定义 SVM 模型出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆