百分比混淆矩阵

时间:2019-11-30 20:41:46

标签: python heatmap confusion-matrix

我创建了用于计算混淆矩阵的函数。我不想在表壳内部的数字上使用热图,而是在行上使用比例。意思

5% 95%
0% 95%

将是

blue red
blue red

而现在,如果第二行上没有数据比例,那么它给出:

blue red
blue blue

您认为有可能吗?

confusion_matrix = sklearn.metrics.confusion_matrix(Y,Y_predict_result)    #we create a raw confusion matrix
cm_sum = np.sum(confusion_matrix, axis=1).reshape(-1,1)    #we get the sum of the lines and reshape it (we re going to use it for the percentage)
cm_percentage = confusion_matrix /  cm_sum.astype(float) * 100    #we get a matrix of percentage. (row proportion for every column)
annot = np.empty_like(confusion_matrix).astype(str)    #we create a raw array for the annotation that we will put on the final result

n_rows, n_cols = confusion_matrix.shape     #getting the size of the matrix
for i in range(0, n_rows):    #here that part is for getting the right annotation at its place.
    for j in range(0, n_cols):     #the idea is that we want, the percentage, then the number that fits in it, and for diagonal elements, the sum of all the elements on the line.
        p = cm_percentage[i, j]
        c = confusion_matrix[i, j]
        if i == j:
            s = cm_sum[i]
            annot[i, j] = '%.1f%%\n%d/%d' % (p, c, s)
        elif c == 0:
            annot[i, j] = ''
        else:
            annot[i, j] = '%.1f%%\n%d' % (p, c)

fig, ax = plt.subplots(figsize=(10,10))    #we set the frame
sns.heatmap(confusion_matrix, annot=annot,fmt='', ax=ax, linewidths=.5, cmap = "coolwarm" )    
#using heatmap and setting some parameter for the confusion matrix.
ax.set_xlabel('Predicted')    #here this line and the next is for putting the meaning of the cases
ax.set_ylabel('True')
ax.set_title('Confusion Matrix')    #title
ax.xaxis.set_ticklabels(labels)    #putting the meaning of each column and row
ax.yaxis.set_ticklabels(labels)
ax.set_ylim(n_rows + 0.1,-0.1)    #expanding the graph... without it, the squares are cut in the middle.
ax.set_xlim(-0.1, n_cols +0.1)
plt.show()
return

0 个答案:

没有答案