8
$\begingroup$

I am trying to do one-class SVM in R. I have been trying to use e1071/ksvm kernlab package. But I am not sure if I am doing it correctly.

Is there any working example for one-class SVM in R?

Also,

  • I am giving a big matrix of predictors as X. Since its supposed to be one-class, is the assumption that all training data I gave forms 'positive' class? If so, we don't have to give the labels 'Y'?
  • The predicted labels given as output are True/False. So I am assuming, True is 'positive' class.

Edit: Attaching sample code. Here I sampled 60% of 'TRUE' class and I tested on the full data set.

library(e1071)
library(caret)

data(iris)

iris$SpeciesClass[iris$Species=="versicolor"] <- "TRUE"
iris$SpeciesClass[iris$Species!="versicolor"] <- "FALSE"
trainPositive<-subset(iris,SpeciesClass=="TRUE")
inTrain<-createDataPartition(1:nrow(trainPositive),p=0.6,list=FALSE)
trainpredictors<-iris[inTrain,1:4]
testpredictors<-iris[,1:4]
testLabels<-iris[,6]

svm.model<-svm(trainpredictors,y=NULL,
               type='one-classification',
               nu=0.5,
               scale=TRUE,
               kernel="radial")
svm.pred<-predict(svm.model,testpredictors)
confusionMatrixTable<-table(Predicted=svm.pred,Reference=testLabels)
confusionMatrix(confusionMatrixTable,positive='TRUE')
$\endgroup$
  • 1
    $\begingroup$ Your assumptions are correct. However, it would be much easier to help you if you would provide some code. $\endgroup$ – Marc Claesen Nov 12 '13 at 19:21
  • $\begingroup$ Thanks Joel for this post, however when applying this code, it gives me very low accuracy than when using the two classes mehtods. Is this true? I am currently comparing both methods to choose which method to use; especially in my case having no data on absences (the other class) and I am currently comparing the 1 class method with 2 class methods (with pseudo-absences). Any help on this regard would be appreciated, Regards. $\endgroup$ – Ahmed El-Gabbas Aug 29 '14 at 15:35
  • $\begingroup$ Cleanest correction of the above code's error is to inTrain line: inTrain<-as.numeric(rownames(trainPositive))[createDataPartition(1:nrow(trainPositive),p=0.6,list=FALSE)] $\endgroup$ – enfascination Sep 5 '17 at 2:23
6
$\begingroup$

The Chapter 9 lab exercise of An Introduction to Statistical Learning provides a working example of using an SVM for binary classification, and it does indeed use the e1071 library. By permission of the publisher, a PDF version of the book is available for free download.

$\endgroup$
2
$\begingroup$

I am providing rectified version of above code. Your 'trainpredictors' selection is wrong because u selected from iris instead of 'trainPositive' but index u selected from 'trainPositive'. Accuracy: train=78.125 test= 91.53

library(e1071)
library(caret)
library(NLP)
library(tm)

data(iris)

iris$SpeciesClass[iris$Species=="versicolor"] <- "TRUE"
iris$SpeciesClass[iris$Species!="versicolor"] <- "FALSE"
trainPositive<-subset(iris,SpeciesClass=="TRUE")
testnegative<-subset(iris,SpeciesClass=="FALSE")
inTrain<-createDataPartition(1:nrow(trainPositive),p=0.6,list=FALSE)

trainpredictors<-trainPositive[inTrain,1:4]
trainLabels<-trainPositive[inTrain,6]

testPositive<-trainPositive[-inTrain,]
testPosNeg<-rbind(testPositive,testnegative)

testpredictors<-testPosNeg[,1:4]
testLabels<-testPosNeg[,6]

svm.model<-svm(trainpredictors,y=NULL,
           type='one-classification',
           nu=0.10,
           scale=TRUE,
           kernel="radial")

svm.predtrain<-predict(svm.model,trainpredictors)
svm.predtest<-predict(svm.model,testpredictors)

confTrain<-table(Predicted=svm.predtrain,Reference=trainLabels)
confTest<-table(Predicted=svm.predtest,Reference=testLabels)

confusionMatrix(confTest,positive='TRUE')

print(confTrain)
print(confTest)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.