путаница матрицы для процента - PullRequest
0 голосов
/ 30 ноября 2019

Я создал эту функцию для вычисления матрицы путаницы. Я хотел бы использовать тепловую карту не по числам внутри корпуса, а по пропорции на линии. Значение,

5% 95%
0% 95%

будет

blue red
blue red

, тогда как теперь, если во второй строке нет данных пропорционально, то это дает:

blue red
blue blue

DoВы думаете, это будет возможно?

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...