如何使用 svm 线性插入符包而不是回归进行分类

How to do Classification using svmLinear carat package and not Regression

我有一个数据集,其标签为 y = 1、2、3、4。我希望使用 R 的 carat 包中提供的 train() 函数通过 svmLinear 方法进行 class化。这是我写的示例代码。我使用了默认的 trainControl 函数。

trainer = train(y~., data=traindf, method="svmLinear")
predicted = predict(trainer, testdf)

但它看起来像是在执行回归而不是 class化,因为预测值不是 1、2、3、4 的离散值。它也有十进制值。 我如何 运行 multi-class classification using svmLinear method of train function.

class 是什么 y?除非 svmLinear 包中有您可以指定输出类型的参数,否则将分析 y 并根据其类型构建模型。至少这是它与插入符号包装的其他监督方法(例如 nnet)一起工作的方式。

简而言之,通过确保 y 的类型为 factor 来解决此问题:

trainer = train(as.factor(y) ~ . -y, data=traindf, method="svmLinear")

更新:
我找不到 Caret 为 linearSVM 包装的特定包的文档,但您也应该能够在参数中指定类型。比如type=C-svc

predict() 也可能是这种情况,您还可以在其中指定类型。例如type = c('prob', 'class')