ARCHIVE Snapshot of the Rätsch lab website (Tübingen, then New York) as of February 2016: preserved, not maintained. The group today: bmi.inf.ethz.ch
Personal tools
Home → Rätsch Lab → Supplements → Shogun → SHOGUN R Examples

SHOGUN R Examples

SVM Classification:

require(graphics)
require(lattice)
library("sg")

meshgrid <- function(a,b) {
      list(
                      x=outer(b*0,a,FUN="+"),
                      y=outer(b,a*0,FUN="+")
              )
}

dims=2;
num=100;

traindat <- matrix(c(rnorm(dims*num)-0.5,rnorm(dims*num)+0.5),dims,2*num)
trainlab <- c(vector(mode="numeric", num)-1, vector(mode="numeric", num)+1)

sg("send_command","loglevel ALL")
sg("set_features", "TRAIN", traindat)
sg("set_labels", "TRAIN", trainlab)
sg("send_command", "set_kernel GAUSSIAN REAL 40 1")
sg("send_command", "init_kernel TRAIN")
sg("send_command", "new_svm LIGHT")
sg("send_command", "c 10.0")
sg("send_command", "svm_train")

x1=(-49:+50)/10
x2=(-49:+50)/10
testdat=meshgrid(x1,x2)
testdat=t(matrix(c(testdat$x, testdat$y),10000,2))

sg("set_features", "TEST", testdat)
sg("send_command", "init_kernel TEST")
out=sg("svm_classify")

z=matrix(out, 100,100)

image(x1,x2,z,col=topo.colors(1000))
contour(x1,x2,z,add=T)
i=which(trainlab==+1);
matplot(traindat[1,i],traindat[2,i],cex=2.0,pch="o", type = "p", col="red",add=T)
i=which(trainlab==-1);
matplot(traindat[1,i],traindat[2,i],cex=2.0,pch="x", type = "p", col="black",add=T)

wireframe(z, shade = TRUE, aspect = c(61/87, 0.4), light.source = c(10,0,10))

SVM Regression:

require(graphics)
library("sg")

traindat <- matrix(1:1000+runif(1000),1,1000)/10
trainlab <- sin(traindat)
testdat <- (matrix(1:1000,1,1000)-1+runif(1000))/10
testlab <- testdat

sg("send_command","loglevel ALL")
sg("set_features", "TRAIN", traindat)
sg("set_labels", "TRAIN", trainlab)
sg("send_command", "set_kernel GAUSSIAN REAL 50 20")
sg("send_command", "init_kernel TRAIN")
sg("send_command", "new_svm LIBSVR")
sg("send_command", "c 0.1")
sg("send_command", "svm_train")
sg("set_features", "TEST", testdat)
sg("set_labels", "TEST", testlab)
sg("send_command", "init_kernel TEST")
out <- sg("svm_classify")
plot(traindat,trainlab, type = "o", pch="x", col="red");
matplot(testdat,out, type = "o", pch="o", col="black",add=T)

Hidden Markov Model:

library(graph)
library(Rgraphviz)
library(sg)

cube=list(NULL,NULL,NULL)
num=vector(mode='numeric',length=18)+100
num[1]=0;
num[2]=0;
num[3]=0;
num[10]=0;
num[11]=0;
num[12]=0;

for (c in 1:3)
{
      for (i in 1:6)
              cube[[c]] <- c(cube[[c]], vector(mode='numeric',length=num[(c-1)*6+i])+i)
      cube[[c]] <- sample(cube[[c]],300,replace=TRUE);
}

x <- c(cube[[1]],cube[[2]],cube[[3]])
x <- paste(x,sep="",collapse="")


sg('set_features','TRAIN',x,'CUBE')
sg('send_command','convert TRAIN STRING CHAR STRING WORD 1')

#train 10 HMM models
liks=vector(mode='numeric', length=10)-Inf
models=vector(mode='pairlist', length=10)
for (i in 1:10)
{
      sg('send_command','new_hmm 3 6')
      sg('send_command','bw')
      liks[[i]] <- sg('hmm_likelihood')
      models[[i]] <- sg('get_hmm')
}

#choose the most likely model and compute viterbi path
h=models[[which(liks==max(liks))]]
sg('set_hmm',h)
sg('set_features','TEST',x,'CUBE')
sg('send_command','convert TEST STRING CHAR STRING WORD 1')
path=sg('get_viterbi_path',0)

p=exp(h$p)
q=exp(h$q)
a=exp(h$a)
b=exp(h$b)

y=c(vector(mode='numeric', length(cube[[1]])),vector(mode='numeric', length(cube[[2]]))+1, vector(mode='numeric', length(cube[[2]]))+2)
matplot(1:length(y), y-0.01,type='l',col='red')
matplot(1:length(path$path), path$path,type='l',col='blue',add=T)
g=new("graphAM",a>1e-6,edgemode = "directed")
plot(g, "neato")
Document Actions