简体   繁体   English

使用e1071而不是kernlab的脱字符的R中的SVM

[英]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. 目前,插入符号火车在引擎盖下使用kernlab svm功能,对于我当前的目的而言,这些速度很慢。 But e1071 svm trainers offer a much needed speed boost. 但是e1071 svm培训师可​​以提供急需的速度提升。 So I would like the cv procedure of caret with svm trainers of e1071. 因此,我想使用e1071的svm培训者来执行插入符号的简历过程。 Is there any way to do that? 有什么办法吗? Basically I want the svm engine of caret to be replaced by e1071 from the default kernlab. 基本上,我想将插入符的svm引擎替换为默认kernlab中的e1071。

I use the following code to train currently. 我目前使用以下代码进行训练。

svm using kernlab 使用kernlab的svm

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

svm using e1071 使用e1071的svm

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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