orange1
orange1

Reputation: 2939

How to extract Accuracy from caret's confusionMatrix?

I'm trying to just extract the Accuracy value from the confusionMatrix() output -- I've tried using the following:

    cl <- train.data[,1]
    knn.res <- knn.cv(train.data[,c(2:783)], cl, k = i, algorithm = "cover_tree")
    confus.knn.res <- confusionMatrix(knn.res, train.data[,1])
    confus.knn.res
    k.accuracy[which(k.accuracy[,2]==i),2] <- confus.knn.res$Accuracy

though just calling it as $Accuracy doesn't seem to work.

Upvotes: 4

Views: 7979

Answers (4)

Majid
Majid

Reputation: 1854

If one only requires the output values (i.e. Overall accuracy value) double brackets should be applied as below:

confus.knn.res$overall[[1]] #Overall accuracy is the first object!

Upvotes: 0

ish
ish

Reputation: 1

Although i am answering very late but still, it can help others to calculate all the parameters required. it can be done by retrieving values from confusion matrix and calculating by the following code:

    conf_train<-table(training$Activity, predictions) #from predicted values


    conf_train<-confusionMatrix(fit.knn,norm = "none")  
#from cross validation of training set, internal
RF.statistics_train = matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), nrow=3, ncol=5) 
colnames(RF.statistics_train )<- c('Precision', 'Sensitivity', 'Specificity', 'Accuracy', 'MCC')
rownames(RF.statistics_train) <- c('Class1', 'Class2', 'Class3')
for(i in 1:3)
{
  TP=conf_train$table[i,i]
  TN=0
  FP=0
  FN=0
  for(j in 1:3)
  {
    if(i!=j)
    {
      FP = FP + conf_train$table[j,i]
      FN = FN + conf_train$table[i,j]
    }
    for(k in 1:3)
    {
      if(i!=j && i!=k)
      {
        TN = TN + conf_train$table[j,k]
      }
    }
  }
  #  statistics[i,1] = conf_test[i,i]/col_total[i]
  #  statistics[i,2] = conf_test[i,i]/row_total[i]
  RF.statistics_train[i,1] = TP/(TP+FP)
  RF.statistics_train[i,2] = TP/(TP+FN)
  RF.statistics_train[i,3] = TN/(TN+FP)
  RF.statistics_train[i,4] = (TP+TN)/(TP+TN+FP+FN)
  RF.statistics_train[i,5] = (TP*TN-FP*FN)/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))`
}

The code is for three class matrix but you can modify accordingly

Upvotes: 0

pbahr
pbahr

Reputation: 1350

Since overall is a named vector, the user-friendly way of doing this would be confus.knn.res$overall["Accuracy"]

Upvotes: 5

orange1
orange1

Reputation: 2939

One of the values of confusionMatrix() object is overall -- the first index of overall is the accuracy value. Therefore, it can be called as confus.knn.res$overall[1].

Upvotes: 8

Related Questions