R中将分类变量与Tune SVM一起使用时出现错误

时间:2019-05-15 09:10:16

标签: r svm

当我想使用tune()函数时,出现一条错误消息,提示我的响应变量必须为数字才能进行回归。

我已经定义了Type = "C",但是错误是定义问题。

obj <- tune(svm, clasifica~., data = SUPERVISADO, scale=FALSE, type="C",
            ranges = list(gamma = 2^(-1:1), cost = 2^(2:4)),
            tunecontrol = tune.control(sampling = "fix")
)
 Error in svm.default(x, y, scale = scale, ..., na.action = na.action) : 
      Need numeric dependent variable for regression.
Error in tune(svm, clasifica ~ ., data = SUPERVISADO, scale = FALSE, type = "C",  :  Dependent variable has wrong type!

1 个答案:

答案 0 :(得分:0)

y必须成为分类的因素。参见?svm

在这个受代码启发的最小工作示例中,请注意对factor()的调用:

library(e1071)

tune(svm, factor(vs) ~ ., 
     data = mtcars, 
     scale = FALSE, 
     ranges = list(gamma = 2^(-1:1), 
     cost = 2^(2:4)),
     tunecontrol = tune.control(sampling = "fix"))

输出:

Parameter tuning of ‘svm’:

- sampling method: fixed training/validation set

- best parameters:
 gamma cost
     1    4

- best performance: 0.2727273