简体   繁体   中英

SVM in R with caret using e1071 instead of kernlab

Currently the caret train uses kernlab svm function under the hood and these are slow for my current purpose. But e1071 svm trainers offer a much needed speed boost. So I would like the cv procedure of caret with svm trainers of e1071. Is there any way to do that? Basically I want the svm engine of caret to be replaced by e1071 from the default kernlab.

I use the following code to train currently.

svm using kernlab

svmModel2 = train(factor(TopPick) ~. - Date , data = trainSet, method = 'svmRadial')
pred.svm2 = predict(svmModel2, testSet)

svm using e1071

svmModel = e1071::svm(factor(TopPick) ~ . - Date, data = trainSet)
pred.svm = predict(svmModel, testSet)

Thanks for the help.

As suggested in comment you can create your own custom model.

svmRadial2ModelInfo <- list(
  label   = "Support Vector Machines with Radial Kernel based on libsvm",
  library = "e1071",
  type    = c("Regression", "Classification"),
  parameters = data.frame(parameter = c("cost", "gamma"),
                          class = c("numeric", "numeric"),
                          label = c("Cost", "Gamma")),
  grid    = function(x, y, len = NULL, search = NULL) {
              sigmas <- kernlab::sigest(as.matrix(x), na.action = na.omit, scaled = TRUE)
              return( expand.grid(gamma = mean(as.vector(sigmas[-2])),
                                  cost  = 2 ^((1:len) - 3)) )
  },
  loop    = NULL,
  fit     = function(x, y, wts, param, lev, last, classProbs, ...) {
              if(any(names(list(...)) == "probability") | is.numeric(y))
              {
                out <- svm(x = as.matrix(x), y = y,
                           kernel = "radial",
                           cost  = param$cost,
                           gamma = param$gamma,
                           ...)
              } else {
                out <- svm(x = as.matrix(x), y = y,
                           kernel = "radial",
                           cost  = param$cost,
                           gamma = param$gamma,
                           probability = classProbs,
                           ...)
              }
              out
  },
  predict = function(modelFit, newdata, submodels = NULL) {
    predict(modelFit, newdata)
  },
  prob    = function(modelFit, newdata, submodels = NULL) {
    out <- predict(modelFit, newdata, probability = TRUE)
    attr(out, "probabilities")
  },
  varImp = NULL,
  predictors = function(x, ...){
    out <- if(!is.null(x$terms)) predictors.terms(x$terms) else x$xNames
    if(is.null(out)) out <- names(attr(x, "scaling")$x.scale$`scaled:center`)
    if(is.null(out)) out <-NA
    out
  },
  levels = function(x) x$levels,
  sort   = function(x) x[order(x$cost, -x$gamma),]
)

Usage:

svmR <- caret::train(x = trainingSet$x,
                     y = trainingSet$y,
                     trControl = caret::trainControl(number=10),
                     method = svmRadial2ModelInfo,
                     tuneLength = 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM