Calculate accuracy for image classification using SVM model

1.6k views Asked by At

I am new to ML and R. I have built a image classification model using SVM. Following is the code I used to build this model,

tuned <- tune.svm(label~., data = train, gamma = 10^(-6:-1), cost = 10^(-1:1))
model  <- svm(label~., data = train, kernel = 'radial', type = 'C-classification', gamma = 0.001, cost = 10)

prediction <- predict(model, test[,-1])
prediction

tab <- table(pred = prediction, true = test[,1])
tab

Is there a function to calculate the accuracy of the model?

I need to know how to generate something like the below screenshot using R, click here for the screenshot

1

There are 1 answers

0
Sandipan Dey On

Try this (svm with 5-fold cross-validation) to get the desired output (ran with randomly generated data)

tuned <- tune.svm(label~., data = train, gamma = 10^(-6:-1), cost = 10^(-1:1)) 
model <- svm(label~., data = train, kernel = 'radial', type = 'C-classification', gamma = 0.001, cost = 10, cross=5)
summary(model)

with output

Call:
svm(formula = label ~ ., data = train, kernel = "radial", type = "C-classification", gamma = 0.001, cost = 10, cross = 5)


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  10 
      gamma:  0.001 

Number of Support Vectors:  70

 ( 52 18 )


Number of Classes:  2 

Levels: 
 false true

5-fold cross-validation on training data:

Total Accuracy: 74.28571 
Single Accuracies:
 57.14286 85.71429 64.28571 92.85714 71.42857 

and then use the model for prediction on the unseen data

prediction <- predict(model, newdata=test[,-1]) 
prediction

tab <- table(pred = prediction, true = test[,1]) 
print('contingency table')
tab

with output 

"contingency table"
       true
pred    false true
  false    21    9
  true      0    0