SVM plot in 3 dimensions

How can I build SVM for a 3D dataset (with x, y, z coordinates)?

I can display 3D data with scatterplot3d(data), but how does it work when using svm results?

Edit: copy from comment to response. This should have been edited by OP:

3 data set

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 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: The last bit looks a bit odd with only three data points.

+3
source share
1 answer

SVM, , , ( - 3D) decision value = 0. 3D rgl , , contour3d() misc3d. :

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)

enter image description hereenter image description here

+12

All Articles