Plot SVM in 3 dimension

data-visualization, machine-learning, r, svm

Solution

For getting the decision boundary for a kernel-transformed SVM, I usually just predict a grid of new data and then fit a contour (or iso-surface in 3D) to the `decision value = 0` level. In 3D you can use the excellent `rgl` package for plotting, like Ben suggested, and the `contour3d()` function from the `misc3d` package. Here's an example:

library(e1071)
library(rgl)
library(misc3d)

n    = 100
nnew = 50

# Simulate some data
set.seed(12345)
group = sample(2, n, replace=T)
dat   = data.frame(group=factor(group), matrix(rnorm(n*3, rep(group, each=3)), ncol=3, byrow=T))

# Fit SVM
fit = svm(group ~ ., data=dat)

# Plot original data
plot3d(dat[,-1], col=dat$group)

# Get decision values for a new data grid
newdat.list = lapply(dat[,-1], function(x) seq(min(x), max(x), len=nnew))
newdat      = expand.grid(newdat.list)
newdat.pred = predict(fit, newdata=newdat, decision.values=T)
newdat.dv   = attr(newdat.pred, 'decision.values')
newdat.dv   = array(newdat.dv, dim=rep(nnew, 3))

# Fit/plot an isosurface to the decision boundary
contour3d(newdat.dv, level=0, x=newdat.list$X1, y=newdat.list$X2, z=newdat.list$X3, add=T)

Problem

How can I plot SVM for a 3D dataset (with x,y,z coordinates)? I am able to plot 3D data by using `scatterplot3d(data)`, but how does it work when using svm results? Edit: copying from comment to answer. This should have been an edit by the OP: 3 set of data ``` data[1:10,1], data[1:10,2] and data[1:10,3] represent genuine data. data[11:15,1], data[11:15,2] and data[11:15,3] represent userA data. data[16:20,1], data[16:20,2] and data[16:20,3] represent userB data. ``` Then I do the SVM with: ``` labels <- matrix( c(rep(1,10), rep(-1, 10)) ) svp <- ksvm(data,labels, type="C-svc" , kernel='rbfdot', C=0.4, kpar=list(sigma=0.2)) ``` Then I have a data test with: ``` dataTest[1,1], dataTest[1,2], dataTest[1,3] predLabels = predict(svp,dataTest) ``` Editor's note: that last bit looks a bit strange with only 3 data points.

Original source