How to check best cost value in SVM

svm
r

#1

I am currently doing a problem of SVM in R and while doing a problem I came across that by changing the value of cost the plot function changes and giving different type of classification .I want to know what should be the value of cost and how to decide it.

set.seed(1)
x<-matrix(rnorm(20*2),ncol=2)
y<-c(rep(-1,10),rep(1,10))
x[y==1,]=x[y==1,]+1
plot(x,col=(3-y))
dat<-data.frame(x=x,y=as.factor(y))
require(e1071)
svmfit=svm(y~.,data=dat,kernel=“linear”,cost=10,scale=FALSE)
plot(svmfit,dat)


svm.fit=svm(y~.,data=dat,kernel=“linear”,cost=0.1,scale=FALSE)
plot(svm.fit,dat)

we can see we are getting different result


#2

Cost function is used when classifying un-balanced classes. If you have two classes, one with 90% instances and other 10%. In this case , if both the classes have the same cost function, the algorithm would simply classify all instances as the 90% to get the best fit.

To avoid such cases you would want to make cost of classifying 10% class wrongly more costly. As in when lesser class one gets wrongly classified the algorithm gets penalized more. This ensure, model is able to deal with classification well despite imbalances.