Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-class classification with SVM in R

I'm using the package e1071 in R in order to build a one-class SVM model. I don't know how to do that and I neither find any example on the Internet.

Could someone give an example code to characterize, for example, the class "setosa" in the "iris" dataset with a one-class classification model and then test all the examples in the same dataset (in order to check what examples belong to the characterization of the "setosa" class and what examples not)?

like image 997
dreamscollector Avatar asked Dec 09 '14 09:12

dreamscollector


People also ask

What is a one class SVM?

One-class SVM is an unsupervised algorithm that learns a decision function for novelty detection: classifying new data as similar or different to the training set.

Can we use SVM for classification?

In its most simple type, SVM doesn't support multiclass classification natively. It supports binary classification and separating data points into two classes. For multiclass classification, the same principle is utilized after breaking down the multiclassification problem into multiple binary classification problems.

What is classification in SVM?

Overview of SVM Classification In SVM Classification, the data can be either linear or non-linear. There are different kernels that can be set in an SVM Classifier. For a linear dataset, we can set the kernel as 'linear'. On the other hand, for a non-linear dataset, there are two kernels, namely 'rbf' and 'polynomial'.

What is 2 class SVM?

An overview of Two Class Support Vector Machine. Two-Class Support Vector Machine is used to create a model that is based on the Support Vector Machine Algorithm. Two-Class Support Vector Machine is used to create a model that is based on the Support Vector Machine Algorithm.


1 Answers

I think this is what you want:

library(e1071)
data(iris)
df <- iris

df <- subset(df ,  Species=='setosa')  #choose only one of the classes

x <- subset(df, select = -Species) #make x variables
y <- df$Species #make y variable(dependent)
model <- svm(x, y,type='one-classification') #train an one-classification model 


print(model)
summary(model) #print summary

# test on the whole set
pred <- predict(model, subset(iris, select=-Species)) #create predictions

Output:

-Summary:

> summary(model)

Call:
svm.default(x = x, y = y, type = "one-classification")


Parameters:
   SVM-Type:  one-classification 
 SVM-Kernel:  radial 
      gamma:  0.25 
         nu:  0.5 

Number of Support Vectors:  27




Number of Classes: 1

-Predictions (only some of the predictions are shown here (where Species=='setosa') for visual reason):

> pred
    1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17    18    19    20    21    22 
 TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE 
   23    24    25    26    27    28    29    30    31    32    33    34    35    36    37    38    39    40    41    42    43    44 
FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE 
   45    46    47    48    49    50 
FALSE  TRUE  TRUE  TRUE  TRUE  TRUE 
like image 112
LyzandeR Avatar answered Sep 22 '22 09:09

LyzandeR