绘制来自 svm 拟合的数据 - 超平面 [英] Plotting data from an svm fit - hyperplane

查看:36
本文介绍了绘制来自 svm 拟合的数据 - 超平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 svm 找到依赖于 q 的超平面最佳拟合回归,其中我有 4 个维度:x、y、z、q.

I used svm to find a hyperplane best fit regression dependent on q, where I have 4 dimensions: x, y, z, q.

fit <- svm(q ~ ., data=data,kernel='linear')

这是我的适合对象:

Call:
svm(formula = q ~ ., data = data, kernel = "linear")


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  linear 
       cost:  1 
      gamma:  0.3333333 

Number of Support Vectors:  1800

我有我的数据的 3d 图,其中第 4 维是颜色,使用 plot3d.如何覆盖 svm 找到的超平面?如何绘制超平面?我想可视化回归超平面.

I have a 3d plot of my data, where the 4th dimension is color, using plot3d. How can I overlay the hyperplane that svm found? How can I plot the hyperplane? I'd like to visualize the regress hyperplane.

推荐答案

您写道:

我使用 svm 找到了超平面最佳拟合回归

I used svm to find a hyperplane best fit regression

但根据:

Call:
svm(formula = q ~ ., data = data, kernel = "linear")

Parameters:
SVM-Type:  C-classification

您正在分类.

所以,首先决定你需要什么:分类或拟合回归,从?svm,我们看到:

So, first of all decide what you need: to classify or to fit regression, from ?svm, we see:

type: ‘svm’ can be used as a classification machine, as a
      regression machine, or for novelty detection.  Depending of
      whether ‘y’ is a factor or not, the default setting for
      ‘type’ is ‘C-classification’ or ‘eps-regression’,
      respectively, but may be overwritten by setting an explicit
      value.

因为我相信您没有更改参数 type 的默认值,您可能正在解决 classification,因此,我将展示如何将其可视化以进行分类.

As I believe you didn't change the parameter type from its default value, you are probably solving classification, so, I will show how to visualize this for classification.

假设有 2 个类,生成一些数据:

Let's assume there are 2 classes, generate some data:

> require(e1071) # for svm()                                                                                                                                                          
> require(rgl) # for 3d graphics.                                                                                                                                                                                    
> set.seed(12345)                                                                                                                                                                     
> seed <- .Random.seed                                                                                                                                                                
> t <- data.frame(x=runif(100), y=runif(100), z=runif(100), cl=NA)
> t$cl <- 2 * t$x + 3 * t$y - 5 * t$z                                                                                                                                                 
> t$cl <- as.factor(ifelse(t$cl>0,1,-1))
> t[1:4,]
           x         y         z cl
 1 0.7209039 0.2944654 0.5885923 -1
 2 0.8757732 0.6172537 0.8925918 -1
 3 0.7609823 0.9742741 0.1237949  1
 4 0.8861246 0.6182120 0.5133090  1

既然你想要 kernel='linear' 边界必须是 w1*x + w2*y + w3*z - w0 - 超平面.我们的任务分为 2 个子任务:1) 评估这个边界平面的方程 2) 绘制这个平面.

Since you want kernel='linear' the boundary must be w1*x + w2*y + w3*z - w0 - hyperplane. Our task divides to 2 subtasks: 1) to evaluate equation of this boundary plane 2) draw this plane.

1) 计算边界平面方程

首先,让我们运行svm():

> svm_model <- svm(cl~x+y+z, t, type='C-classification', kernel='linear',scale=FALSE)

我在这里明确写了 type=C-classification 只是为了强调我们要分类.scale=FALSE 意味着我们希望 svm() 直接使用提供的数据运行而不缩放数据(默认情况下).我这样做是为了将来的评估变得更简单.

I wrote here explicitly type=C-classification just for emphasis we want do classification. scale=FALSE means that we want svm() to run directly with provided data without scaling data (as it does by default). I did it for future evaluations that become simpler.

不幸的是,svm_model 不存储边界平面的方程(或者只是它的法向量),所以我们必须评估它.从 svm-algorithm 我们知道我们可以使用以下公式评估这些权重:

Unfortunately, svm_model doesn't store the equation of boundary plane (or just, normal vector of it), so we must evaluate it. From svm-algorithm we know that we can evaluate such weights with following formula:

w <- t(svm_model$coefs) %*% svm_model$SV

负截距存储在svm_model中,通过svm_model$rho访问.

The negative intercept is stored in svm_model, and accessed via svm_model$rho.

2) 绘图平面.

我没有找到任何有用的函数plane3d,所以,我们应该再次做一些方便的工作.我们只是取对网格 (x,y) 并评估边界平面的 z 的适当值.

I didn't find any helpful function plane3d, so, again we should do some handy work. We just take grid of pairs (x,y) and evaluate the appropriate value of z of the boundary plane.

detalization <- 100                                                                                                                                                                 
grid <- expand.grid(seq(from=min(t$x),to=max(t$x),length.out=detalization),                                                                                                         
                    seq(from=min(t$y),to=max(t$y),length.out=detalization))                                                                                                         
z <- (svm_model$rho- w[1,1]*grid[,1] - w[1,2]*grid[,2]) / w[1,3]

plot3d(grid[,1],grid[,2],z)  # this will draw plane.
# adding of points to the graphics.
points3d(t$x[which(t$cl==-1)], t$y[which(t$cl==-1)], t$z[which(t$cl==-1)], col='red')
points3d(t$x[which(t$cl==1)], t$y[which(t$cl==1)], t$z[which(t$cl==1)], col='blue')

我们用 rgl 包做到了,你可以旋转这张图片并享受它:)

We did it with rgl package, you can rotate this image and enjoy it :)

这篇关于绘制来自 svm 拟合的数据 - 超平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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