How to Build Multiclass Svm in R

How to build multiclass SVM in R?

There is no direct equivalent of Multiclass SVM in e1071. Besides, all approaches to use SVM for multiclass classification use techniques like 'one vs rest' or encoding, amongst others. Here is a reference detailing most common approaches...
http://arxiv.org/ftp/arxiv/papers/0802/0802.2411.pdf

If you want to use e1071 for multiclass SVM, you best can create 26 svm models, one for each class, and use the probability score to predict. This approach should be good enough for handwritten pattern recognition.

How to perform multi-class classification using 'svm' of e1071 package in R

The iris dataset contains three class labels: "Iris setosa", "Iris virginica" and "Iris versicolor". To employ a balanced one-against-one classification strategy with svm, you could train three binary classifiers:

The first classifier's training set only contains the "Iris setosa" and "Iris virginica" instances. The second classifier's training set only contains the "Iris setosa" and the "Iris versicolor" instances. The third classifier's training set--I guess by now you'll know already--contains only the "Iris virginica" and the "Iris versicolor" instances.

To classify an unknown instance, you apply all three classifiers. A simple voting strategy could then select the most frequently assigned class label, a more sophisticated may also consider the svm confidence scores for each assigned class label.

Edit (This principle works out of the box with svm):

# install.packages( 'e1071' )
library( 'e1071' )
data( iris )
model <- svm( iris$Species~., iris )
res <- predict( model, newdata=iris )

How to test data with R e1071 SVM multiclass

Factors in train and test dataset are different here so you would need to fix it first.

library(e1071)
#sample data
train_data <- data.frame(V1 = c("sunny","sunny sunny","rainy","rainy rainy","cloud","cloud cloud"),
V2= c("yes","yes","no","no","maybe","maybe"))
test_data <- data.frame(V1 = c("sunny","rainy","hello","cloud"))

#fix levels in train_data & test_data dataset before running model
train_data$ind <- "train"
test_data$ind <- "test"
merged_data <- rbind(train_data[,-grep("V2", colnames(train_data))],test_data)
#train data
train <- merged_data[merged_data$ind=="train",]
train$V2 <- train_data$V2
train <- train[,-grep("ind", colnames(train))]
#test data
test <- merged_data[merged_data$ind=="test",]
test <- data.frame(V1 = test[,-grep("ind", colnames(test))])

#svm model
svm_model <- svm(V2 ~ ., data = train, probability=TRUE)
summary(svm_model)
train_pred <- predict(svm_model,train["V1"])
table(train_pred,train$V2)

#prediction on test data
test$test_pred <- predict(svm_model,test)
test

Hope this helps!

One-class classification with SVM in R

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


Related Topics



Leave a reply



Submit