R: How do we print percentage accuracy for SVM
Asked Answered
A

1

5

Here is my sample R code:

    train <- read.csv("Train.csv")
    test <- read.csv("Test+.csv")

   x <- model.matrix(age ~ . - 1,data=train)           

    classify=svm(as.factor(age)~ ., data=train,method="class")
    pred = predict(classify,test,type="class")

How could I print percentage accuracy from this? I want to display all the performance metrics like accuracy, precision, recall .etc for my evaluation.

Aurilia answered 28/3, 2016 at 19:52 Comment(2)
Define accuracy--there are multiple valid accuracy metrics for classification.Sceptic
I don't think it applies for a classification problem. An outcome is either in a category or it's not.Galactopoietic
C
8

Here are a couple of options, using the built-in iris data frame for illustration:

library(e1071)

m1 <- svm(Species ~ ., data = iris)

Create confusion matrix using table function:

table(predict(m1), iris$Species, dnn=c("Prediction", "Actual"))   
            Actual
Prediction   setosa versicolor virginica
setosa         50          0         0
versicolor      0         48         2
virginica       0          2        48

Use the caret package to generate the confusion matrix and other model diagnostics (you can also use caret for the entire model development, tuning and validation process):

library(caret)

confusionMatrix(iris$Species, predict(m1))
Confusion Matrix and Statistics

            Reference
Prediction   setosa versicolor virginica
setosa         50          0         0
versicolor      0         48         2
virginica       0          2        48

Overall Statistics

Accuracy : 0.9733          
95% CI : (0.9331, 0.9927)
No Information Rate : 0.3333          
P-Value [Acc > NIR] : < 2.2e-16       

Kappa : 0.96            
Mcnemar's Test P-Value : NA              

Statistics by Class:

                     Class: setosa Class: versicolor Class: virginica
Sensitivity                 1.0000            0.9600           0.9600
Specificity                 1.0000            0.9800           0.9800
Pos Pred Value              1.0000            0.9600           0.9600
Neg Pred Value              1.0000            0.9800           0.9800
Prevalence                  0.3333            0.3333           0.3333
Detection Rate              0.3333            0.3200           0.3200
Detection Prevalence        0.3333            0.3333           0.3333
Balanced Accuracy           1.0000            0.9700           0.9700
Czarevitch answered 28/3, 2016 at 20:18 Comment(1)
@Aurilia "how to tune" is a completely different question from "how to print percentage accuracy".Playful

© 2022 - 2024 — McMap. All rights reserved.