SVM for iris in R

Tags:

library(e1071)
data(iris)

set.seed(12345) 
sample_idx <- sample(nrow(iris), nrow(iris) * 0.7)
train <- iris[sample_idx, ]
test <- iris[-sample_idx, -5]
lable <- iris[-sample_idx, 5]

# Using default parameters.
svm.model <- svm(Species ~ ., data=train, probability=TRUE)

# Getting prob and label.
predicted.prob <- attr(predict(svm.model, test, probability=TRUE), "probabilities")
predicted.label <- colnames(predicted.prob)[max.col(predicted.prob)]

# To check that the code for finding the max is right, uncomment the below.
# table(predicted.label, predict(svm.model, test))

# Estimate accuracy.
confusion_matrix <- table(predicted.label, lable)
confusion_matrix
classAgreement(confusion_matrix)$diag

# Tune parameters.
svm.tuned.model <- tune.svm(Species ~ .,
                            data=train,
                            type='C-classification',
                            kernel='radial',
                            cost=2^(-5:5),
                            gamma=2^(-5:5),
                            tunecontrol=tune.control(cross=10))$best.model

predicted.tuned.lable <- predict(svm.tuned.model, test)

# Estimate accuracy.
confusion_matrix.tuned <- table(predicted.tuned.lable, lable)
confusion_matrix.tuned
classAgreement(confusion_matrix.tuned)$diag

Here’s output.

Loading required package: class
               lable
predicted.label setosa versicolor virginica
     setosa         13          0         0
     versicolor      0         15         3
     virginica       0          0        14
[1] 0.9333333
                     lable
predicted.tuned.lable setosa versicolor virginica
           setosa         13          0         0
           versicolor      0         15         2
           virginica       0          0        15
[1] 0.9555556