【问题标题】:Error with custom SVM model for tuning in caret用于调整插入符号的自定义 SVM 模型出错
【发布时间】:2015-03-05 20:37:00
【问题描述】:

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

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

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

即使我逐字复制链接中的代码,我也会收到类似的错误,我不知道如何解决它。我找到了this link,它介绍了自定义模型的构建方式,我看到了这个错误的引用位置,但仍然不确定问题是什么。我的代码如下:

#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

【问题讨论】:

    标签: r svm


    【解决方案1】:

    查看提供的second link 后,我决定尝试在模型的参数中包含一个标签,从而解决了问题!有趣的是它起作用了,因为插入符号文档说该值是可选的,但如果它起作用,我不能抱怨。

    #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)
    

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 2013-11-15
      • 2016-06-29
      • 2015-12-17
      • 1970-01-01
      • 2018-01-14
      • 2015-08-03
      • 2018-10-29
      相关资源
      最近更新 更多