本文展示绘制混淆矩阵过程中可用的几乎所有颜色属性
1.Perceptually Uniform Sequential(多色渐变) 系列
其一(viridis)展示:
2.Sequential (同色渐变)系列
其一(binary)展示:
3.Diverging colormaps(双色渐变)系列
其一(PiYG)展示:
4.Qualitative(可爱卡通) 颜色系列
其一(Pastel1)展示:
5.Miscellaneous(大杂烩)颜色系列
其一(flag)展示:
混淆矩阵绘制源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | from sklearn.metrics import confusion_matrix import numpy as np import matplotlib.pyplot as plt import itertools def plot_confusion_matrix(cm, target_names, title='Confusion matrix', cmap=plt.cm.Reds,#设置混淆矩阵的颜色主题 normalize=True): accuracy = np.trace(cm) / float(np.sum(cm)) misclass = 1 - accuracy if cmap is None: cmap = plt.get_cmap('Blues') plt.figure(figsize=(15, 12)) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() if target_names is not None: tick_marks = np.arange(len(target_names)) plt.xticks(tick_marks, target_names, rotation=45) plt.yticks(tick_marks, target_names) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] thresh = cm.max() / 1.5 if normalize else cm.max() / 2 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): if normalize: plt.text(j, i, "{:0.4f}".format(cm[i, j]), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") else: plt.text(j, i, "{:,}".format(cm[i, j]), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass)) plt.show() def plot_confuse(model, x_val, y_val, labels): predictions = model.predict_classes(x_val,batch_size=batch) truelabel = y_val.argmax(axis=-1) # 将one-hot转化为label conf_mat = confusion_matrix(y_true=truelabel, y_pred=predictions) plt.figure() plot_confusion_matrix(conf_mat, normalize=False,target_names=labels,title='Confusion Matrix') |
参考:
Keras绘制混淆矩阵
python 画图colorbar颜色大全