[R] svm(e1071) and scaling of weights

Juergen Rose rose at rz.uni-potsdam.de
Wed Feb 23 17:35:38 CET 2011


I expected, that I will get the same prediction, if I multiply the
weights for all classes with a constant factor, but I got different
results. Please look for the following code.

> library(e1071)
> data(Glass, package = "mlbench")
> index <- 1:nrow(Glass)

> testindex <- sample(index, trunc(length(index)/5))
> testset <- Glass[testindex, ]
> trainset <- Glass[-testindex, ]

> datatrain <- subset(trainset,select=-Type)
> classestrain  <- subset(trainset,select=Type)
> Wts <- 1.0/table(Glass$Type);

> model <-
svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=100,class.weights=Wts)

> datatest = subset(testset,select=-Type)
> classestest <- subset(testset,select=Type)
> pred.test=predict(model,datatest)

> table.test <- table(pred.test,t(classestest))
> table.test
         
pred.test  1  2  3  5  6  7
        1 11  2  0  0  0  0
        2  1 12  0  0  0  0
        3  1  3  1  0  0  0
        5  0  0  0  1  0  0
        6  0  1  0  0  1  0
        7  0  1  0  1  0  6

That is the first prediction. Now I multiply all weights by 100, and
create a new model and make the next prediction:


> Wts <- 100.0/table(Glass$Type);
> model <-
svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=100,class.weights=Wts)
> pred.test=predict(model,datatest)
> table.test <- table(pred.test,t(classestest))
> table.test
         
pred.test  1  2  3  5  6  7
        1 10  5  0  0  0  0
        2  2 13  0  0  0  0
        3  1  1  1  0  0  0
        5  0  0  0  1  0  0
        6  0  0  0  0  1  0
        7  0  0  0  1  0  6

This prediction differs from the last prediction. Is this a numerical
inaccuracy or is this a expected result?

Then I observed, if I take the larger weights, but divide the cost by
100, I get again the first prediction:


> model <-
svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts)
> pred.test=predict(model,datatest)
> table.test <- table(pred.test,t(classestest))
> table.test
         
pred.test  1  2  3  5  6  7
        1 11  2  0  0  0  0
        2  1 12  0  0  0  0
        3  1  3  1  0  0  0
        5  0  0  0  1  0  0
        6  0  1  0  0  1  0
        7  0  1  0  1  0  6

Is this accidentally or an expected result?



More information about the R-help mailing list