2016-09-29 67 views
3

是否有內置的方法可以單獨獲得每個班級的準確性分數?我知道在sklearn中我們可以通過使用metric.accuracy_score來獲得整體的準確性。有沒有辦法讓每個班級的精確度得分?類似於metrics.classification_reportScikit-learn,獲得每個班級的準確性分數

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'] 

classification_report沒有給出準確的分數:

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 

的準確度得分只有給人的整體精度:

accuracy_score(y_true, y_pred) 
Out[10]: 0.59999999999999998 

回答

2

你可以自己編寫它:精度無非是多良好分類的樣品(真陽性和真陰性)與您擁有的樣品總數之間的比率。

然後,對於給定的班級,而不是考慮所有的樣本,你只考慮你的班級。

然後你可以試試這個: 讓我們先定義一個方便的函數。上述

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 

該函數將返回索引列表中的某個值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)) 

最後一個函數將返回在類的準確性,你找的

+0

我不知道在numpy中是否有一個現有函數返回列表中與您的參數匹配的值的索引。有人有想法嗎?非常感謝 ! – MMF