一:
con_mat = confusion_matrix(Y_test, Y_pred)
classes = ['N', 'A', 'V', 'L', 'R']
plt.figure(figsize=(8, 8))
seaborn.heatmap(con_mat, annot=True, fmt='.20g', cmap='BrBG_r')
plt.title('confusion_matrix')
plt.ylim(0, 5)
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()
二
from sklearn.metrics import confusion_matrix
from sklearn.metrics import recall_score
import matplotlib.pyplot as plt
guess = [1, 0, 1, 2, 1, 0, 1, 0, 1, 0]
fact = [0, 1, 0, 1, 2, 1, 0, 1, 0, 1]
classes = list(set(fact))
classes.sort()
confusion = confusion_matrix(guess, fact)
plt.imshow(confusion, cmap = plt.cm.Blues)
indices = range(len(confusion))
plt.xticks(indices, classes)
plt.yticks(indices, classes)
plt.colorbar()
plt.xlabel('guess')
plt.ylabel('fact')
for first_index in range(len(confusion)):
for second_index in range(len(confusion[first_index])):
plt.text(first_index, second_index, confusion[first_index][second_index],va = 'center',ha = 'center')
plt.show()
三
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)
plt.title('confusion_matrix')
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes)
plt.yticks(tick_marks, classes)
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]))
plt.ylabel('Real label')
plt.xlabel('Prediction')
plt.tight_layout()
plt.show()