首页 文章

在插入符号中调整自定义SVM模型时出错

提问于
浏览
1

我在插入包中使用自定义训练模型时遇到问题 . 我需要进行SVM回归,我想找到SVM模型的所有参数 - cost,sigma和epsilon . 内置版本只有成本和西格玛 . 我已经找到了非常有用的提示herehere但我的模型仍然无效 .

Error in models$grid(x = x, y = y, len = tuneLength, search = trControl$search) : 
unused argument (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

我找不到任何人,他们有同样的错误,不知道究竟是什么错 . 这段代码几乎就是我在网上找到并略有改动的东西 .

顺便说一下这是我的第一篇文章,所以希望这是可以理解的,如果不是,我可以添加其他信息 .

1 回答

  • 2

    解决方案是在网格函数中包含一个参数 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自定义功能仔细,你会看到插入符号要你指定如何在情况下,用户想要做网格搜索选择默认参数的情况下,她想要做随机搜索(见"the grid element") .

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

    这可能更容易看到:

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

相关问题