Você está na página 1de 5

Support Vector Machine with Multiple Classes

YIK LUN, KEI


allen29@ucla.edu
This paper is a lab from the book called An Introduction to Statistical Learning
with Applications in R. All R codes and comments below are belonged to the
book and authors.

0
2
4

x[,2]

library(e1071)
set.seed(1)
x=matrix(rnorm(200*2),ncol=2);y=c(rep(1,150),rep(2,50))
x[1:100,]=x[1:100,]+2;x[101:150,]= x[101:150,]-2
x=rbind(x,matrix(rnorm(50*2),ncol =2));y=c(y,rep(0 ,50))
x[y==0,2]= x[y==0,2]+2
dat=data.frame(x=x, y=as.factor(y))
plot(x,col =(y+1))

0
x[,1]

SVM
svmfit=svm(y~., data=dat , kernel ="radial", cost =10, gamma =1)
plot(svmfit , dat)

SVM classification plot

2
x

x
oo oo o o
x
o
o
o
o
o oo
ooooo o
o oooo ooooo
oo oo
o
o
o
x
x
xx x o
oo o ooooo ooo x
2
x xoo
x xx
xx
x xx xoo oooo oox
x
x
o x
x xx x ox xx xxxxxx x x x xx xx
x x x x x x x o xx
x
ooo o x xx xxxxxx x
x
x
0
x
x o
o
x x x xx oxx xxxxxxx xx x ooo oo o
x
o
x
x
o
o
o
x xx xxo xx
x x
o
o x
o o oo o
xx ooo
o
o
o
o
o
2
ooooo o ox
o
o
o
x
oo oo oo oo oooo x
x
x
x
o o
o
o
o o

x.1

x
o

x.2

tune
set.seed (1)
tune.out=tune(svm , y~., data=dat, kernel ="radial",
ranges =list(cost=c(0.1 ,1 ,10 ,100 ,1000),
gamma=c(0.5,1,2,3,4) ))
tune.out$best.model
##
## Call:
## best.tune(method = svm, train.x = y ~ ., data = dat, ranges = list(cost = c(0.1,
##
1, 10, 100, 1000), gamma = c(0.5, 1, 2, 3, 4)), kernel = "radial")
##
##
## Parameters:
##
SVM-Type: C-classification
2

## SVM-Kernel: radial
##
cost: 1
##
gamma: 0.5
##
## Number of Support Vectors:

125

svmfit=svm(y~., data=dat , kernel ="radial", cost =10, gamma =2)


plot(svmfit , dat)

SVM classification plot

x.2

Gene Expression Data


library(ISLR)
dat=data.frame(x=Khan$xtrain , y=as.factor(Khan$ytrain))
out=svm(y~., data=dat , kernel ="linear",cost =10)
summary(out)

##
## Call:
## svm(formula = y ~ ., data = dat, kernel = "linear", cost = 10)
##
##
## Parameters:
3

2
x

x
oo oo o o
o ooo
x oo o o oo
x
o o oo ooo
ooooo
o
o
o
xxx xx o o
x
o
oo o ooooo oxo x
2
x oxxoo
x x
o
x
xx
x xx xoo oooo oo
x
x
o x
x
x
o x ox xx xxxoxx x x x xx ox
x x x xxx x x x x o xx
oo
x
o
x
x x xo o x xx xx x x
0
x
x x xxxxxxx x x o oo
x
x
x
o
x
x
x
o
x
x
o
o
x
x
oo
x
x xx ooo xx
o
x
x
x
o o
x o oo
xx xxx
o
o
x
o
x
o
2
x
o
o
x
o oo oxo
x
oo xx oo oo oooo x
x
x
x
o o
x
x o
x

x.1

x
o

##
SVM-Type: C-classification
## SVM-Kernel: linear
##
cost: 10
##
gamma: 0.0004332756
##
## Number of Support Vectors: 58
##
## ( 20 20 11 7 )
##
##
## Number of Classes: 4
##
## Levels:
## 1 2 3 4
dat.te=data.frame(x=Khan$xtest , y=as.factor (Khan$ytest ))
pred.te=predict (out , newdata =dat.te)
table(pred.te,dat.te$y)
##
## pred.te 1 2 3 4
##
1 3 0 0 0
##
2 0 6 2 0
##
3 0 0 4 0
##
4 0 0 0 5
tune.out=tune(svm , y~., data=dat, kernel ="linear",
ranges =list(cost=c(0.1 ,1 ,10)))
tune.out$best.model
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
best.tune(method = svm, train.x = y ~ ., data = dat, ranges = list(cost = c(0.1,
1, 10)), kernel = "linear")

Parameters:
SVM-Type:
SVM-Kernel:
cost:
gamma:

C-classification
linear
0.1
0.0004332756

Number of Support Vectors:

58

out=svm(y~., data=dat , kernel ="linear",cost =0.1)


dat.te=data.frame(x=Khan$xtest , y=as.factor (Khan$ytest))
pred.te=predict (out , newdata =dat.te)
table(pred.te,dat.te$y)
##
## pred.te 1 2 3 4
##
1 3 0 0 0
4

##
##
##

2 0 6 2 0
3 0 0 4 0
4 0 0 0 5

Reference:
James, Gareth, et al. An introduction to statistical learning. New
York: springer, 2013.

Você também pode gostar