Juergen Rose
2011-Feb-23 17:12 UTC
[R] repeated execution of svm(e1071) gives different results, if probability = TRUE is set
I called svm and predict three times with the same data and got three differing predictions:> library(e1071)Lade n?tiges Paket: class> data(Glass, package = "mlbench") > index <- 1:nrow(Glass) > testindex <- sample(index, trunc(length(index)/5)) > testset <- Glass[testindex, ] > trainset <- Glass[-testindex, ] > model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred_new.test <- predict(model,datatest,probability = TRUE) > table.test <- table(pred_new.test,t(classestest)) > table.testpred_new.test 1 2 3 5 6 7 1 12 3 1 0 0 0 2 1 15 0 0 0 0 3 0 0 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 1 0 7 0 1 0 1 0 6> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred_new.test <- predict(model,datatest,probability = TRUE) > table.test <- table(pred_new.test,t(classestest)) > table.testpred_new.test 1 2 3 5 6 7 1 12 3 1 0 0 0 2 1 14 0 0 0 0 3 0 0 0 0 0 0 5 0 0 0 1 0 0 6 0 1 0 0 1 0 7 0 1 0 1 0 6> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred_new.test <- predict(model,datatest,probability = TRUE) > table.test <- table(pred_new.test,t(classestest)) > table.testpred_new.test 1 2 3 5 6 7 1 12 3 1 0 0 0 2 1 13 0 0 0 0 3 0 0 0 0 0 0 5 0 0 0 1 0 0 6 0 1 0 0 1 0 7 0 2 0 1 0 6 Is this a feature or a bug?
Juergen Rose
2011-Feb-24 12:15 UTC
[R] repeated execution of svm(e1071) gives different results, if weights are use and if probability = TRUE is set
Am Mittwoch, den 23.02.2011, 18:12 +0100 schrieb Juergen Rose:> class.weights=Wts,I have just seen, that me last code was not complete. I try it once more:> 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)If I now repeat:> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1)> pred.test=predict(model,datatest); table(pred.test,t(classestest))I allways get the same result: pred.test 1 2 3 5 6 7 1 12 1 4 0 0 0 2 0 11 3 2 0 0 3 0 0 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 2 0 7 0 0 0 0 0 6 Also if I repeat:> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) I get the same result as above. But if I set:> Wts <- 1.0/table(Glass$Type)and if I then repeat:> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) each attempt provides a different result:> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) pred.test 1 2 3 5 6 7 1 0 3 3 3 2 6 2 12 9 4 0 0 0 3 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) pred.test 1 2 3 5 6 7 1 12 12 7 3 2 6 2 0 0 0 0 0 0 3 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) pred.test 1 2 3 5 6 7 1 12 9 6 0 1 0 2 0 3 1 3 1 6 3 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) pred.test 1 2 3 5 6 7 1 12 12 7 3 2 6 2 0 0 0 0 0 0 3 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0> model <-svm(datatrain,classestrain,type="C-classification",kernel="radial",cost=1,class.weights=Wts,probability=TRUE)> pred.test=predict(model,datatest,probability=TRUE);table(pred.test,t(classestest)) pred.test 1 2 3 5 6 7 1 10 11 3 3 0 3 2 2 1 4 0 2 3 3 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0 What I am doing wrong?