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

查看:54
本文介绍了如何在 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)

我无法使绘图命令起作用.我想要这样的图表 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.

这应该有效...

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天全站免登陆