Scikit-learn, get accuracy scores for each class
<p>Is there a built-in way for getting accuracy scores for each class separatetly? I know in sklearn we can get overall accuracy by using <code>metric.accuracy_score</code>. Is there a way to get the breakdown of accuracy scores for individual classes? Something similar to <code>metrics.classification_report</code>.</p>

<pre><code>from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
</code></pre>

<p><code>classification_report</code> does not give accuracy scores:</p>

<pre><code>print(classification_report(y_true, y_pred, target_names=target_names, digits=4))

Out[9]:         precision    recall  f1-score   support

class 0     0.5000    1.0000    0.6667         1
class 1     0.0000    0.0000    0.0000         1
class 2     1.0000    0.6667    0.8000         3

avg / total     0.7000    0.6000    0.6133         5
</code></pre>

<p>Accuracy score gives only the overall accuracy:</p>

<pre><code>accuracy_score(y_true, y_pred)
Out[10]: 0.59999999999999998
</code></pre>

<p>You can code it by yourself : the accuracy is nothing more than the ratio between the well classified samples (true positives and true negatives) and the total number of samples you have.</p>

<p>Then, for a given class, instead of considering all the samples, you only take into account those of your class.</p>

<p>You can then try this:
Let's first define a handy function.</p>

<pre><code>def indices(l, val):
   retval = []
   last = 0
   while val in l[last:]:
           i = l[last:].index(val)
           retval.append(last + i)
           last += i + 1   
   return retval
</code></pre>

<p>The function above will return the indices in the list <strong>l</strong> of a certain value <strong>val</strong></p>

<pre><code>def class_accuracy(y_pred, y_true, class):
    index = indices(l, class)
    y_pred, y_true = ypred[index], y_true[index]
    tp = [1 for k in range(len(y_pred)) if y_true[k]==y_pred[k]]
    tp = np.sum(tp)
    return tp/float(len(y_pred))
</code></pre>

<p>The last function will return the in-class accuracy that you look for.</p>

python machine-learning scikit-learn