【问题标题】:Error with tuning custom SVM model in caret在插入符号中调整自定义 SVM 模型时出错
【发布时间】:2017-08-18 13:58:30
【问题描述】:

我在使用 caret 包中的自定义训练模型时遇到了问题。我需要做一个 SVM 回归,我想找到 SVM 模型的所有参数——成本、西格玛和 epsilon。内置版本只有 cost 和 sigma。我已经找到了非常有用的提示herehere,但我的模型仍然无法正常工作。

models$grid(x = x, y = y, len = tuneLength, search = trControl$search) 中的错误: 未使用的参数(搜索 = trControl$search)

这个错误是我得到的,我的代码在这里。

SVMrbf <- list(type = "Regression", library = "kernlab", loop = NULL)
prmrbf <- data.frame(parameters = data.frame(parameter = c('sigma', 'C', 'epsilon'),
                                         class = c("numeric", "numeric", "numeric"),
                                         label = c('Sigma', "Cost", "epsilon")))
SVMrbf$parameters <- prmrbf
svmGridrbf <- function(x, y, len = NULL) {
                  library(kernlab)
                  sigmas <- sigest(as.matrix(x), na.action = na.omit, scaled = TRUE, frac = 1)
                  expand.grid(sigma = mean(sigmas[-2]), epsilon = 10^(-5:0),
          C = 2 ^(-5:len)) # len = tuneLength in train
}
SVMrbf$grid <- svmGridrbf
svmFitrbf <- function(x, y, wts, param, lev, last, weights, classProbs, ...) {
                   ksvm(x = as.matrix(x), y = y,
                         type = "eps-svr",
                         kernel = "rbfdot",
                         sigma = param$sigma,
                         C = param$C, epsilon = param$epsilon,
                         prob.model = classProbs,
                         ...)
}
SVMrbf$fit <- svmFitrbf
svmPredrbf <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
  predict(modelFit, newdata)
SVMrbf$predict <- svmPredrbf
svmProb <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
  predict(modelFit, newdata, type="probabilities")
SVMrbf$prob <- svmProb
svmSortrbf <- function(x) x[order(x$C), ]
SVMrbf$sort <- svmSortrbf


svmRbfFit <- train(x = train.predictors1, y = train.response1, method =       SVMrbf,
                 tuneLength = 10)
svmRbfFit

我找不到任何人,他们有同样的错误,但不知道到底出了什么问题。这段代码几乎是我在网上找到的,稍作改动。

顺便说一句,这是我的第一篇文章,所以希望它是可以理解的,如果不是我可以添加更多信息。

【问题讨论】:

    标签: r svm r-caret kernlab


    【解决方案1】:

    解决方案是在您的网格函数中包含一个参数search,例如使用

    svmGridrbf <- function(x, y, len = NULL, search = "grid") {
                      library(kernlab)
                      sigmas <- sigest(as.matrix(x), na.action = na.omit, scaled = TRUE, frac = 1)
                      expand.grid(sigma = mean(sigmas[-2]), epsilon = 10^(-5:0), C = 2 ^(-5:len)) # len = tuneLength in train
    }
    

    如果您仔细查看 caret documentation 的自定义函数,您会发现插入符号希望您指定如何选择默认参数以防用户想要进行网格搜索以防万一她想做随机搜索(参见“网格元素”)。

    错误消息告诉您插入符号将参数传递给函数,该函数实际上并未定义为该函数的参数。

    这里可能更容易看到:

    sd(x = c(1,2,3), a = 2)
    # Error in sd(x = c(1, 2, 3), a = 2) : unused argument (a = 2)
    

    【讨论】:

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