sklearn.metrics中的confusion_matrix、ROC、AUC指標


1.confusion_matrix

理論部分見https://www.cnblogs.com/cxq1126/p/12990784.html#_label2

 1 from sklearn.metrics import confusion_matrix
 2 
 3 #if y_true.shape=y_pred.shape=(N,)
 4 tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()   
 5 print('sensitivity: ', tp/(tp+fn))
 6 print('specificity: ', tn/(tn+fp))
 7 
 8 #if y_true.shape=y_pred.shape=(N, 2)
 9 tn, fp, fn, tp = confusion_matrix(y_true2[:, 0], y_pred2[:, 0], labels=[0,1]).ravel()
10 print('sensitivity: ', tp/(tp+fn))
11 print('specificity: ', tn/(tn+fp))

2.classification_report

1 from sklearn.metrics import classification_report
2 
3 file_logger.info('classification report:\n%s' % classification_report(y_true, y_pred, target_names=test_dataset.ind_to_cls_dict, digits=4))

y_true和y_pred的shape=(N,),如果y_pred.shape=(N, 2),先進行此操作torch.argmax(pred_y, dim=1);

調用結果類似下面

3.roc_curve, auc 

如果最后的y_score維度是(N, )(即經過網絡層的輸出概率logits的shape=(N, ),也就是說最后的fc層輸出維度為1),畫一個ROC曲線

 1 from sklearn.metrics import roc_curve, auc 
 2 
 3 fpr, tpr, threshold = roc_curve(y_true, y_score)
 4 roc_auc = auc(fpr, tpr)
 5         
 6 plt.figure(figsize=(8, 5))
 7 plt.plot(fpr, tpr, color='darkorange', label='ROC curve (area = %0.4f)' % roc_auc)  
 8  
 9 lw = 2   
10 plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
11 plt.xlim([0.0, 1.0])
12 plt.ylim([0.0, 1.05])
13 plt.xlabel('False Positive Rate')
14 plt.ylabel('True Positive Rate')
15 plt.legend(loc="lower right")
16 plt.show()

Tip:y_pred的類型是np.array

如果最后的y_score維度是(N, 2)(即經過網絡層的輸出概率logits的shape=(N, 2),也就是說最后的fc層輸出維度為2),按類別畫2個ROC曲線

 1 from sklearn.metrics import roc_curve, auc 
 2 import matplotlib.pyplot as plt
 3 
 4 plt.figure(figsize=(8, 5))
 5 colors = ['darkorange', 'cornflowerblue']
 6 fpr, tpr, roc_auc = dict(), dict(), dict()
 7 for i in range(2):
 8      fpr[i], tpr[i], threshold = roc_curve(y_true2[:, i], y_score[:, i])
 9      roc_auc[i] = auc(fpr[i], tpr[i])
10         
11     
12      plt.plot(fpr[i], tpr[i], color=colors[i], label='ROC curve (area = %0.4f)' % roc_auc[i])  
13  
14 lw = 2   
15 plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
16 plt.xlim([0.0, 1.0])
17 plt.ylim([0.0, 1.05])
18 plt.xlabel('1-Specificity')
19 plt.ylabel('Sensitivity')
20 plt.legend(loc="lower right")
21 plt.show()

如果維度(N,)想要轉換成(N, 2),可以使用獨熱編碼,詳細見https://www.cnblogs.com/cxq1126/p/13696082.html#_label3

1 import torch.nn.functional as F
2 
3 #y_true改成二維版本
4 x1 = F.one_hot(torch.tensor(y_true), num_classes = 2)
5 y_true2 = np.array(x1)

然后再調用roc_curve函數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM