导入包
import pandas as pd
import numpy as np
from sklearn import metrics
import itertools
import matplotlib.pyplot as plt
读取文件
df = pd.read_csv('xxx.csv')
wrong_labels = df['wrong_labels'].tolist()
right_labels = df['right_labels'].tolist()
my_confusion_matrix = metrics.confusion_matrix(np.array(wrong_labels), np.array(right_labels), labels=None, sample_weight=None)
labels = list(set(right_labels))
绘制混淆矩阵
def plot_Matrix(cm, classes, title=None, cmap=plt.cm.Blues):
plt.rc('font',family='sans-serif',size='4.5')
plt.rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans', 'SimHei', 'Lucida Grande', 'Verdana']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(200, 200))
plt.rcParams['figure.dpi'] = 200
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
if int(cm[i, j]*100 + 0.5) == 0:
cm[i, j]=0
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.figure.colorbar(im, ax=ax)
plt.title('Confusion matrix')
ax.set(xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
xticklabels=list(range(len(classes))), yticklabels=list(range(len(classes))),
title=title,
ylabel='Actual',
xlabel='Predicted')
ax.set_xticks(np.arange(cm.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(cm.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="gray", linestyle='-', linewidth=0.05)
ax.tick_params(which="minor", bottom=False, left=False)
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
fmt = 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
if int(cm[i, j]*100 + 0.5) > 0:
ax.text(j, i, format(int(cm[i, j]*100 + 0.5) , fmt) + '%',
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
plt.show()
plot_Matrix(my_confusion_matrix, labels, title='Normalized confusion matrix')
result
