I'm using table
to show results from the kmeans
cluster vs. the actual class values.
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 %
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With