如何在R中绘制SVM的分类图 [英] How do I plot a classification graph of a SVM in R

查看:427
本文介绍了如何在R中绘制SVM的分类图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个SVM,现在我想绘制这台机器的分类空间。我在互联网上发现了一些例子,但我似乎无法理解它们。

I have an SVM in R and I would now like to plot the classification space for this machine. I have found some examples on the Internet, but I can't seem to make sense of them.

我的R脚本如下所示:

library(e1071)
day_of_week <- c(0,1,2,3,4,5,6)
holiday <- factor( c(T, F, F, F, F, F, T) )
model <- svm(day_of_week, holiday)
plot(model, day_of_week, holiday)

我无法获得plot命令的工作。我想要一个像这样的图 http://bm2.genes.nig.ac.jp/RGM2/R_current/library/e1071/man/images/plot.svm_001.png

I cannot get the plot command to work. I would like a graph something like this http://bm2.genes.nig.ac.jp/RGM2/R_current/library/e1071/man/images/plot.svm_001.png

推荐答案

首先, plot.svm 函数假定数据在两个维度上变化。您在示例中使用的数据仅为一维,因此决策边界必须绘制在一条不支持的线上。其次,该功能似乎需要一个数据框架作为输入,并且您正在使用矢量。

First of all, the plot.svm function assumes that the data varies across two dimensions. The data you have used in your example is only one-dimensional and so the decision boundary would have to be plotted on a line, which isn't supported. Secondly, the function seems to need a data frame as input and you are working with vectors.

这应该可行......

This should work...

library(e1071)

day = c(0,1,2,3,4,5,6)
weather = c(1,0,0,0,0,0,0)
happy = factor(c(T,F,F,F,F,F,F))

d = data.frame(day=day, weather=weather, happy=happy)
model = svm(happy ~ day + weather, data = d)
plot(model, d)

这篇关于如何在R中绘制SVM的分类图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆