Yue Y
Yue Y

Reputation: 593

R: Tuning SVM parameter - class.weights in {e1071} package

I wanted to training a svm classifier with package {e1071}. I realize that class.weight is one of the parameters I wanted to tune. Eg. I want to test two class weights c(25, 50) vs. c(20, 55) I wonder if the build in tune function does the job, and if so, how?

Here is my training data:

training.data  = 

  height0 height1 height2 weight0 weight1 gender class
1     0      1       0       1       0      1       1
2     0      1       0       0       1      0       1
3     0      1       0       0       0      1       1
4     1      0       0       1       0      0       1
5     0      1       0       0       1      0       2
6     0      1       0       0       1      0       2

and there are 2 levels in the response variable 'class'

training.data$class = 

[1] 1 1 1 1 2 2
Levels: 1 2

I want to use a function like this,

param.obj <- tune(svm, class ~., data = training.data, 
    ranges = list("1" = c(25, 20), "2" = c(50,55) ),
    tunecontrol = tune.control(sampling = "cross", cross = 5) )

but I don't think this is the correct way to do it, because if I change "2" to "3" it still works.

param.obj <- tune(svm, class ~., data = training.data, 
    ranges = list("1" = c(25, 20), "3" = c(50,55) ),
    tunecontrol = tune.control(sampling = "cross", cross = 5) )

doesn't give me an error. I Googled around but cannot seem to find the proper way... Any help is appreciated!

Upvotes: 2

Views: 4728

Answers (1)

Wayne
Wayne

Reputation: 973

The ranges list is a named list of parameters and the parameter you want to adjust is class.weights. I believe your ranges line in the tune would be something like:

ranges=list (class.weights=list(c("1"=25, "2"=20), c("1"=50, "2"=55))

Upvotes: 4

Related Questions