# Calculate accuracy using true/false positives/negatives

I got

predicted = [0, 0, 1, 0, 1, 2, 1, 0, 1, 0]
actual    = [1, 2, 1, 2, 0, 1, 0, 2, 1, 1]

from multiclass classifier

Next, I calculate for 3 classes

tp = [0, 2, 0]
fp = [5, 2, 1]
tn = [3, 3, 6]
fn = [2, 3, 3]

The real accuracy is 0.2, but I got 14/30 using formula

accuracy = (tp+tn)/(tp+fp+tn+fn)

How to calculate accuracy correctly?

You are confused about the terminology. The terms "false positive" and "false negative" are only used in binary classification. You have 3 classes, so, these terms aren't applicable.

However, we still can calculate the accuracy directly from two vectors. Here is some Python code to do it:

import numpy as np
predicted = [0, 0, 1, 0, 1, 2, 1, 0, 1, 0]
actual    = [1, 2, 1, 2, 0, 1, 0, 2, 1, 1]

sum(np.array(predicted)==np.array(actual))/float(len(actual))

On the other hand, if we define "positive" in "one vs. all" setting, then we will have accuracy in difference classes (because we will have multiple binary classifier). In this case, we will have 3 accuracy numbers for each class in stead of one accuracy number.

• So, there is no way to calculate accuracy, precision and recall using this terms for multiclass classificator? – Bogdan Ruzhitskiy Mar 15 '17 at 16:14
• @BogdanRuzhitskiy in multiclass problem how to define "positive"? if those terms are not defined. how to calculate fp, fn, etc.? – Haitao Du Mar 15 '17 at 16:17
• I considered a problem of K class classification as K problems of binary classification, like "one against all". In that way, I can define "positive" and "negative" terms – Bogdan Ruzhitskiy Mar 15 '17 at 16:29
• @BogdanRuzhitskiy if you define "one vs. all", then you will have accuracy in difference classes. i.e., you will have 3 accuracy numbers for each class. – Haitao Du Mar 15 '17 at 16:36