【问题标题】:How to plot test data into svm.plot of R package e1071?如何将测试数据绘制到 R 包 e1071 的 svm.plot 中?
【发布时间】:2017-08-19 01:23:51
【问题描述】:

我使用 R 包 e1071 和 iris 数据集构建了一个 SVM 模型。我已将 iris 数据拆分为训练和测试数据,并从训练数据构建 SVM。现在我想使用points 函数将测试数据绘制到显示训练数据和 SVM 边界的同一图中。但是,当我这样做时,相当多的测试数据点最终会出现在绘图右侧之外,考虑到绘图比例和测试数据的实际值,这应该是不可能的。这是我使用的代码:

# Import e1071 package and iris data set
library(e1071)
data("iris")

# Split the data into training and test data
index <- c(1:35, 51:85, 101:135)
iris_training <- iris[index, ]
iris_test <- iris[-index, ]

# Compute SVM model with linear kernel
svm_model <- svm(Species ~ Petal.Length + Petal.Width, 
                 data=iris_training,
                 kernel='linear')

# Plot the data and decision boundaries
plot(svm_model, 
     data = iris_training[ , c(3, 4, 5)],
     xlim = c(0.05, 2.55),
     ylim = c(0.9, 7), 
     svSymbol = 1, 
     dataSymbol = 1, 
     symbolPalette = c("black", "blue", "red"))

# Plot the test data into the same plot
points(iris_test$Petal.Width, 
       iris_test$Petal.Length,
       pch="x")

任何想法如何解决此问题或如何解决此问题?非常感谢帮助。

【问题讨论】:

    标签: r plot svm test-data


    【解决方案1】:

    好的,所以这种东西就在我的小巷里,但我一直在寻找各种可能的方法,没有什么可以解释的。但是,如果您需要的话,我想出了一种方法来让这个数字看起来正确。 (仍然没有解释为什么会发生这个错误。)我做到了:iris_test$Petal.Width[31:45] &lt;- iris_test$Petal.Width[31:45] - 0.3 并绘制。它可以为您提供所需的身材。我真的不确定为什么这是必要的。我调整了我能想到的一切,却一无所获。

    【讨论】:

    • 非常感谢@jwells。我需要演示文稿的情节,因此您建议的修复将解决我的问题。顺便说一句,我必须减去 0.7 而不是 0.3 才能使情节看起来正确。不确定这是否提供了任何新的线索来理解这种绘图行为。但似乎这可能是 R 开发人员中的某个人可能想要查看的问题。再次感谢您的帮助。
    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 2012-01-07
    • 2016-06-21
    • 2011-11-15
    • 2013-11-04
    • 2020-01-05
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多