当前位置:网站首页>Draw confusion matrix

Draw confusion matrix

2022-06-10 17:40:00 Guoqingru

Obtain confusion matrix

end y_test,y_pred They are one-dimensional test data set label list 、 List of one-dimensional model prediction results

from sklearn.metrics import confusion_matrix  #  Introduce the corresponding library 
cnf_matrix = confusion_matrix(y_test,y_pred)

Get a two-dimensional matrix :
 Insert picture description here

The drawing of confusion matrix ( Detailed instructions ):

 Insert picture description here

class = ['A','B','C','D','E']  # Category label 
confusion_matrix = [[9 1 3 4 0 0]    
[2,13,1,3,4]
[1 4 10 0 13]
[3 1 1 17 0]
[0 0 0 1 14]]
# Confusion matrix 
#confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
classes = ['A','B','C','D','E']
confusion_matrix = np.array([(9,1,3,4,0),(2,13,1,3,4),(1,4,10,0,13),(3,1,1,17,0),(0,0,0,1,14)],dtype=np.float64)

plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Oranges)  # The matrix is displayed in pixels 
plt.title('confusion_matrix')
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=-45)
plt.yticks(tick_marks, classes)

thresh = confusion_matrix.max() / 2.
#iters = [[i,j] for i in range(len(classes)) for j in range((classes))]
#ij pairing , Traversal matrix iterator 
iters = np.reshape([[[i,j] for j in range(5)] for i in range(5)],(confusion_matrix.size,2))
for i, j in iters:
    plt.text(j, i, format(confusion_matrix[i, j]))   # Display the corresponding number 

plt.ylabel('Real label')
plt.xlabel('Prediction')
plt.tight_layout()
plt.show()

The operation effect is as follows :
 Insert picture description here

原网站

版权声明
本文为[Guoqingru]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101637303776.html