简体   繁体   English

R:如何在 ggplot2 中绘制 svm 的超平面和边距?

[英]R: How to plot the hyperplane and margins of an svm in ggplot2?

I'm following along Tibshirani's ISL text.我正在关注 Tibshirani 的 ISL 文本。 I'm trying to plot the results of an SVM in ggplot2.我正在尝试在 ggplot2 中绘制 SVM 的结果。 I can get the points and the support vectors, but I can't figure out how to get the margins and hyperplane drawn for the 2D case.我可以获得点和支持向量,但我无法弄清楚如何获得为 2D 情况绘制的边距和超平面。 I Googled and checked the e1071 readme.我用谷歌搜索并检查了 e1071 自述文件。 A general, dynamic solution (applicable to a variety of SVM kernels,costs,etc.) would be great.通用的动态解决方案(适用于各种 SVM 内核、成本等)会很棒。 Here is my MWE:这是我的 MWE:

set.seed(1)
N=20
x=matrix(rnorm(n=N*2), ncol=2)
y=c(rep(-1,N/2), rep(1,N/2))
x[y==1,] = x[y==1,] + 1;x[y==1,]
dat = data.frame(x=x, y=as.factor(y))
library(e1071)
library(ggplot2)
svmfit=svm(y~., data=dat, kernel="linear", cost=10, scale=FALSE)

df = dat; df
df = cbind(df, sv=rep(0,nrow(df)))
df[svmfit$index,]$sv = 1

ggplot(data=df,aes(x=x.1,y=x.2,group=y,color=y)) +     
    geom_point(aes(shape=factor(sv)))

Something like this:像这样的东西: 在此处输入图片说明 (From Python's scikit-learn) (来自 Python 的 scikit-learn)

So you don't want to plot the support vectors right?所以你不想绘制支持向量对吗? Here's something very basic that works for your example, based on the plot.svm source code.这是基于plot.svm源代码的适用于您的示例的非常基本的东西。

https://github.com/cran/e1071/blob/master/R/svm.R https://github.com/cran/e1071/blob/master/R/svm.R

You can construct something much richer by taking a look at that source code.您可以通过查看该源代码来构建更丰富的内容。

library(e1071)
library(ggplot2)
set.seed(1)
N=20
x=matrix(rnorm(n=N*2), ncol=2)
y=c(rep(-1,N/2), rep(1,N/2))
x[y==1,] = x[y==1,] + 1;x[y==1,]
dat = data.frame(x=x, y=as.factor(y))
svmfit=svm(y~., data=dat, kernel="linear", cost=10, scale=FALSE)

grid <- expand.grid(seq(min(dat[, 1]), max(dat[, 1]),length.out=100),                                                                                                         
                            seq(min(dat[, 2]), max(dat[, 2]),length.out=100)) 
names(grid) <- names(dat)[1:2]
preds <- predict(svmfit, grid)
df <- data.frame(grid, preds)
ggplot(df, aes(x = x.2, y = x.1, fill = preds)) + geom_tile()

Should output this:应该输出这个:

在此处输入图片说明

Compare this to the plot.svm output:将此与plot.svm输出进行比较:

plot(svmfit, dat)

在此处输入图片说明

EDIT:编辑:

If you want to reproduce the points as well, I've altered the above code slightly:如果你也想重现这些点,我对上面的代码做了一些改动:

cols <- c('1' = 'red', '-1' = 'black')
tiles <- c('1' = 'magenta', '-1' = 'cyan')
shapes <- c('support' = 4, 'notsupport' = 1)
dat$support <- 'notsupport'
dat[svmfit$index, 'support'] <- 'support'

ggplot(df, aes(x = x.2, y = x.1)) + geom_tile(aes(fill = preds)) + 
  scale_fill_manual(values = tiles) +
  geom_point(data = dat, aes(color = y, shape = support), size = 2) +
  scale_color_manual(values = cols) +
  scale_shape_manual(values = shapes) +
  ggtitle('SVM classification plot')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM