2
$\begingroup$

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?

$\endgroup$

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ So, there is no way to calculate accuracy, precision and recall using this terms for multiclass classificator? $\endgroup$ Commented Mar 15, 2017 at 16:14
  • $\begingroup$ @BogdanRuzhitskiy in multiclass problem how to define "positive"? if those terms are not defined. how to calculate fp, fn, etc.? $\endgroup$
    – Haitao Du
    Commented Mar 15, 2017 at 16:17
  • $\begingroup$ 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 $\endgroup$ Commented Mar 15, 2017 at 16:29
  • $\begingroup$ @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. $\endgroup$
    – Haitao Du
    Commented Mar 15, 2017 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.