# Examples for One class SVM in R

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,
svm.pred<-predict(svm.model,testpredictors)
confusionMatrixTable<-table(Predicted=svm.pred,Reference=testLabels)
confusionMatrix(confusionMatrixTable,positive='TRUE')

• Your assumptions are correct. However, it would be much easier to help you if you would provide some code. – Marc Claesen Nov 12 '13 at 19:21
• 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. – Ahmed El-Gabbas Aug 29 '14 at 15:35
• 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)] – enfascination Sep 5 '17 at 2:23

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.

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,