簡體   English   中英

在插入符號中調整自定義 SVM 模型時出錯

[英]Error with tuning custom SVM model in caret

我的插入符號包中的自定義訓練模型有問題。 我需要做一個 SVM 回歸,我想找到 SVM 模型的所有參數 - 成本、西格瑪和 epsilon。 內置版本只有cost和sigma。 我已經在這里這里找到了一個非常有用的提示,但我的模型仍然不起作用。

模型 $grid(x = x, y = y, len = tuneLength, search = trControl$search) 中的錯誤:未使用的參數 (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

我找不到任何人,誰有同樣的錯誤,不知道什么是真正的錯誤。 這段代碼幾乎只是我在網上找到的並略有改動。

順便說一句,這是我的第一篇文章,所以希望它是可以理解的,如果不是,我可以添加其他信息。

解決方案是在您的網格函數中包含一個參數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 文檔,您會看到 caret 希望您指定如何選擇默認參數,以防用戶想要進行網格搜索以及她想要進行隨機搜索(請參閱“網格元素”)。

錯誤消息告訴您,插入符號將一個參數傳遞給該函數,該參數實際上並未定義為該函數的參數。

這可能更容易在這里看到:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM