Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate accuracy from table matrix

Tags:

r

k-means

I'm using table to show results from the kmeans cluster vs. the actual class values.

enter image description here

How can I calculate the % accuracy based on that table. I know how to do it manually.

Iris-setosa had all 50 in cluster 2 while Iris-versicolor had two in the other cluster.

Is there a way to calculate the % like Incorrectly classified instances: 52%

I would like to print the confusion matrix by classes and clusters. Something lke this:

   0   1  <-- assigned to cluster
 380 120 | 1
 135 133 | 0

Cluster 0 <-- 1
Cluster 1 <-- 0

Incorrectly clustered instances :   255.0    33.2031 %
like image 432
birdy Avatar asked Sep 29 '22 09:09

birdy


1 Answers

You can use diag() to select the cases on the diagonal and use that to calculate (in)accuracy as shown below:

sum(diag(d))/sum(d) #overall accuracy
1-sum(diag(d))/sum(d) #incorrect classification 

You can also use this to calculate the number of cases (in)correctly classified:

sum(diag(d)) #N cases correctly classified
sum(d)-sum(diag(d)) #N cases incorrectly classified

where d is your confusion matrix

like image 162
User7598 Avatar answered Oct 06 '22 00:10

User7598