An SV Classifier Example  classification data set. 2

advertisement

An SV Classifier Example

Here is a small fake p

 2 classification data set.

x1 x2 y

1.4 0.7 1

1.4 1.8 1

1.5 2.5 1

0.7 4.0 1

2.2 1.0 1

1.7 1.8 1

1.7 3.3 2

1.4 4.0 2

2.8 0.9 1

3.6 2.2 2

3.1 2.7 2

2.7 4.3 1

3.9 1.3 2

4.1 1.9 2

3.8 3.0 2

3.8 3.8 2

Using the svm

routine in the e1071

package with code like

#This is the fake data set for the svm Example x1<-c(1.4,1.4,1.5,0.7,2.2,1.7,1.7,1.4,2.8,3.6,3.1,2.7,3.9,4.1,3.8,3.8) x2<-c(0.7,1.8,2.5,4.0,1.0,1.8,3.3,4.0,0.9,2.2,2.7,4.3,1.3,1.9,3.0,3.8) y<-c(1,1,1,1,1,1,2,2,1,2,2,1,2,2,2,2) y<-as.factor(y) dat<-data.frame(x1,x2,y) x<-cbind(x1,x2) plot(x1,x2,col=c(as.integer(as.numeric(y)+1)),

pch=19,xlim=c(0,5),ylim=c(0,5)) cbind(x1,x2,y) dat

#Load the e1071 Package and then fit the SV Classifier

S<-svm(y~.,data=dat,kernel="linear",cost=1,scale=FALSE)

S beta = drop(t(S$coefs) %*% x[S$index, ]) beta

S$rho abline(S$rho/beta[2],-beta[1]/beta[2]) abline((S$rho-1)/beta[2],-beta[1]/beta[2]) abline((S$rho+1)/beta[2],-beta[1]/beta[2])

M<-1/sqrt(beta[1]^2+beta[2]^2)

M

S$index

S$coefs

Snumber<-length(S$index)

Snumber

Beta<-c(0,0) for (i in 1:Snumber) {Beta<-Beta+S$coefs[i]*dat[S$index[i],1:2]}

Beta one can get plots that (after using a drawing program) look about like these below for 2 different values of the cost parameter

C

* .

Download