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 metric.accuracy_score
. Is
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.
Then, for a given class, instead of considering all the samples, you only take into account those of your class.
You can then try this: Let's first define a handy function.
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
The function above will return the indices in the list l of a certain value val
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))
The last function will return the in-class accuracy that you look for.