我想绘制一个混淆矩阵的图片,类似于这种效果的:我在网上找到了代码,但是读不懂,具体不知道怎么操作。我现在数据都有,计算好的矩阵也有,原数据的分类情况什么的都有,可就是不知...
我想绘制一个混淆矩阵的图片,类似于这种效果的:
我在网上找到了代码,但是读不懂,具体不知道怎么操作。我现在数据都有,计算好的矩阵也有,原数据的分类情况什么的都有,可就是不知道怎么用Python操作,请熟悉Python的高手帮我解答一下,十分感谢!
代码如下:
from sklearn.metrics import confusion_matriximport matplotlib.pyplot as pltimport numpy as np#load labels.labels = []file = open('labels.txt', 'r')lines = file.readlines()for line in lines: labels.append(line.strip())file.close()
y_true = []y_pred = []#load true and predict labels.file = open('predict.txt', 'r')lines = file.readlines()for line in lines: y_true.append(int(line.split(" ")[1].strip())) y_pred.append(int(line.split(" ")[0].strip()))file.close()tick_marks = np.array(range(len(labels))) + 0.5def plot_confusion_matrix(cm, title='Confusion Matrix', cmap = plt.cm.binary): plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() xlocations = np.array(range(len(labels))) plt.xticks(xlocations, labels, rotation=90) plt.yticks(xlocations, labels) plt.ylabel('True label') plt.xlabel('Predicted label')cm = confusion_matrix(y_true, y_pred)np.set_printoptions(precision=2)cm_normalized = cm.astype('float')/cm.sum(axis=0)[:, np.newaxis]
(字数限制,剩下的部分贴在追问里了。)
英文方面我懂,简单的语句,符号啥的,也懂一些。但是就是不知道具体怎么操作,要把数据贴到代码的某些位置呢,还是贴到txt文件里,然后直接让Python调用呢,调用的画,路径怎么写,写在哪儿?等等操作的问题。
plt.figure(figsize=(12,8), dpi=120)
#set the fontsize of label.
#for label in plt.gca().xaxis.get_ticklabels():
# label.set_fontsize(8)
#text portion
()
展开