1. 程式人生 > 其它 >機器學習中的評價標準

機器學習中的評價標準

from sklearn.metrics import classification_report
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))

輸出:
                precision    recall  f1-score   support

     class 0       0.50      1.00      0.67         1
     class 1       0.00      0.00      0.00         1
     class 2       1.00      0.67      0.80         3

    accuracy                           0.60         5
   macro avg       0.50      0.56      0.49         5
weighted avg       0.70      0.60      0.61         5
 

其中列表左邊的一列為分類的標籤名,右邊support列為每個標籤的出現次數.precision recall f1-score三列分別為各個類別的精確度/召回率及 F1值.

  • TP:預測為1(Positive),實際也為1(Truth-預測對了)
  • TN: 預測為0(Negative),實際也為0(Truth-預測對了)
  • FP: 預測為1(Positive),實際為0(False-預測錯了)
  • FN: 預測為0(Negative),實際為1(False-預測錯了)

Accuracy = (TP+TN) ⁄(TP + FP + TN + FN)

Precision =TP ⁄(TP+FP)

Recall = TP ⁄(TP+FN)

Precision又叫查準率,Recall又叫查全率

F1 = 2Precision *Recall ⁄(Precision+Recall)

macro avg 為算術平均值,以precision為例,macro avg = (0.50 + 0 + 1.00) ⁄ 3 = 0.50

weighted avg 是用每一個類別樣本數量在所有類別的樣本總數的佔比作為權重 以precision為例, weighted avg = (0.5*1 + 0*1 + 1*3)/5 = 0.7

此外還有micro avg

 

 

from sklearn.metrics import confusion_matrix, precision_score

y_true = ["A", "A", "A", "A", "B", "B", "C", "C", "C", "C", "C"]
y_pred = ["A", "B", "A", "A", "B", "A", "B", "C", "C", "C", "C"]
print(confusion_matrix(y_true, y_pred))
print(precision_score(y_true, y_pred, average='micro'))

[[3 1 0] [1 1 0] [0 1 4]]
0.7272727272727273

對於類別A,它的TP=3, FP=1;對於類別B,它的TP=1, FP=1;對於類別C,它的TP=4,FP=1,因此micro avg precision為:

(3+1+4)/(3+1+1+1+4+1)=0.7273