我如何在 R 中使用插入符号绘制多标签 SVM 问题的决策边界

How can i plot the decision boundary for a multilabel SVM problem using caret in R

我已经使用 R 的插入符包构建了 SVM 模型:

set.seed(1234567)


SVM_caret <- train(x = x_train, y = y_train$label, 
               method = "svmLinear", tuneGrid = expand.grid(
              C = c(0.001, 0.01, 0.1, 1)),
              metric = "ROC",
              trControl = trainControl(method = "cv", number = 3, classProbs = T),
              maxit = 100)

我试图实现 this 代码来绘制模型的决策边界,但我遇到了几个错误。这是模型的混淆矩阵:

Confusion Matrix and Statistics

          Reference
Prediction class_1 class_2 class_3 class_4
   class_1       9       0       0       0
   class_2       0       7       0       0
   class_3       3       0       6       0
   class_4       0       0       0       7

Overall Statistics

               Accuracy : 0.9062          
                 95% CI : (0.7498, 0.9802)
    No Information Rate : 0.375           
    P-Value [Acc > NIR] : 5.706e-10       

                  Kappa : 0.8743          

 Mcnemar's Test P-Value : NA              

Statistics by Class:

                     Class: class_1 Class: class_2 Class: class_3 Class: class_4
Sensitivity                  0.7500         1.0000         1.0000         1.0000
Specificity                  1.0000         1.0000         0.8846         1.0000
Pos Pred Value               1.0000         1.0000         0.6667         1.0000
Neg Pred Value               0.8696         1.0000         1.0000         1.0000
Prevalence                   0.3750         0.2188         0.1875         0.2188
Detection Rate               0.2812         0.2188         0.1875         0.2188
Detection Prevalence         0.2812         0.2188         0.2812         0.2188
Balanced Accuracy            0.8750         1.0000         0.9423         1.0000

有4个类可以预测,我不知道这个地块是否真的有可能建成,但我不知道如何完成。有没有什么函数或方法可以可视化这个决策边界??

因为我不能只复制粘贴我的数据,我会在 google 驱动器上添加一个 link 到它,所以你可以下载并重现问题,别担心对于尺寸,因为它真的很轻。

以下是 link:

train_data

train_labels

非常感谢您的帮助。

如评论所述:

当您有两个预测变量时,您只能在二维图中可视化决策边界。但是,您使用的是 10 个预测变量,这意味着每个点都存在于 10 维 space 中,这无法按照您打算的方式绘制。

选择预测变量子集进行绘图将使您能够绘制决策边界,但它们不会以任何有意义的方式划分您绘图中的数据。

如果您确实想要可视化一组决策规则,您可以制作决策树。


dtree <- train(x = svm_data[,-1], y = svm_labels$label, 
                   method = "rpart",
                   metric = "Accuracy",
                   trControl = trainControl(method = "cv", number = 3, classProbs = T),
                   cp = 0.005,
                   maxdepth = 3)



plot(dtree$finalModel, margin = 0.2)
text(dtree$finalModel)