Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How do we print percentage accuracy for SVM

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.

like image 807
Mahsolid Avatar asked Mar 28 '16 19:03

Mahsolid


People also ask

What is accuracy and Kappa in R?

The Kappa Coefficient can be used to evaluate the accuracy of a classification. It evaluates how well the classification performs compared to map, in which all values are just randomly assigned. The Kappa coefficient can range from -1 to 1. A value of 0 indicates that the classification is as good as random values.

What is Kappa in Caret output?

Kappa is similar to Accuracy score, but it takes into account the accuracy that would have happened anyway through random predictions. Kappa = (Observed Accuracy – Expected Accuracy) / (1 – Expected Accuracy) Cohen's kappa is shown as an output of caret's confusionMatrix function.


1 Answers

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
like image 140
eipi10 Avatar answered Nov 01 '22 11:11

eipi10