python中如何用字符串轴而不是整数绘制混淆矩阵
我正在学习上一篇关于如何在Matplotlib中绘制混淆矩阵的文章。脚本如下:python中如何用字符串轴而不是整数绘制混淆矩阵,python,matplotlib,Python,Matplotlib,我正在学习上一篇关于如何在Matplotlib中绘制混淆矩阵的文章。脚本如下: from numpy import * import matplotlib.pyplot as plt from pylab import * conf_arr = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,0,0,0,0,0], [0,4,41,0,0,0,0,0,0,0,1], [0,1,0,30,0,6,0,0,0,0,1], [0,0,0,0,38,10,0,0,0,
from numpy import *
import matplotlib.pyplot as plt
from pylab import *
conf_arr = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,0,0,0,0,0], [0,4,41,0,0,0,0,0,0,0,1], [0,1,0,30,0,6,0,0,0,0,1], [0,0,0,0,38,10,0,0,0,0,0], [0,0,0,3,1,39,0,0,0,0,4], [0,2,2,0,4,1,31,0,0,0,2], [0,1,0,0,0,0,0,36,0,2,0], [0,0,0,0,0,0,1,5,37,5,1], [3,0,0,0,0,0,0,0,0,39,0], [0,0,0,0,0,0,0,0,0,0,38] ]
norm_conf = []
for i in conf_arr:
a = 0
tmp_arr = []
a = sum(i,0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
for i,j in ((x,y) for x in xrange(len(conf_arr))
for y in xrange(len(conf_arr[0]))):
ax.annotate(str(conf_arr[i][j]),xy=(i,j))
cb = fig.colorbar(res)
savefig("confusion_matrix.png", format="png")
我想改变轴来显示字母串,比如(A,B,C,…)而不是整数(0,1,2,3,…10)。一个人怎么能做到这一点。谢谢
musa只需使用和使用
例如
我猜你想要的是: 以下是您想要的:
from string import ascii_uppercase
from pandas import DataFrame
import numpy as np
import seaborn as sn
from sklearn.metrics import confusion_matrix
y_test = np.array([1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5])
predic = np.array([1,2,4,3,5, 1,2,4,3,5, 1,2,3,4,4])
columns = ['class %s' %(i) for i in list(ascii_uppercase)[0:len(np.unique(y_test))]]
confm = confusion_matrix(y_test, predic)
df_cm = DataFrame(confm, index=columns, columns=columns)
ax = sn.heatmap(df_cm, cmap='Oranges', annot=True)
图像输出示例如下:
如果您想要一个更完整的混淆矩阵作为matlab的默认值,包括每个单元格上的总数(最后一行和最后一列)和百分比,请参见下面的模块 因为我浏览了互联网,没有在python上找到像这样的混淆矩阵,我用这些改进开发了一个,并在git上共享
参考: 输出示例如下所示:
如果您的结果存储在csv文件中,您可以直接使用此方法,否则您可能需要进行一些更改以适应结果的结构 从以下内容修改示例: 输出将类似于:
要获得sklearn为您创建的图形,只需使用他们的代码即可
从sklearn.metrics导入混淆矩阵
#我使用sklearn度量源进行此操作
从sklearn.metrics导入混淆矩阵显示
classNames=np.arange(1,6)
#转换为混淆矩阵的离散值
regPredictionsCut=pd.cut(regPredictionsTDF[0],bin=5,labels=classNames,right=False)
cm=混淆矩阵(y检验,regPredictionsCut)
disp=混淆矩阵显示(混淆矩阵=cm,显示标签=类名)
显示绘图()
我通过点击“源”链接找到了答案
以下是结果图:
感谢Joe提供的解决方案。我采纳了你的建议,但我得到了一个替换的数字。我使用的是python版本python 2.6。4@user729470-嗯,你不能只是复制粘贴它然后让它工作。查看
xticks
和yticks
采用的参数。第一个是记号的位置,第二个是标签列表。在上面的示例中,我在[0,1,2,3,4]
处放置了记号。在您的情况下,您希望在不同的位置显示刻度。如果您只是复制粘贴上面的代码,它会将记号放在范围(5)
指定的位置。感谢Joe提供的解决方案。我采纳了你的建议,但我得到了一个替换的数字。我使用的是python版本python 2.6.4。我得到的情节是。我想得到下面的绘图@user729470-如果你只是复制粘贴我上面的内容,是的,正如我所解释的,这将发生。您不希望将记号放在0,1,2,3,4,而是希望它们放在其他位置(范围(0,10,2),在您的情况下
)。您需要调整示例以适应您的情况。或者,如果您不想更改记号的位置,而只想更新标签本身,则可以使用ax.setxticklabels
。@JoeKington-我正在试图理解您的脚本。但是,我意识到另一个问题,即画布没有正确缩放,因此轴标签和记号被切断。在axis标签中,您的图表看起来非常完美。请参见中保存的图。有办法解决这个问题吗?您使用的是什么版本的matplotlib?我使用的是最新版本,结果正确。如果x轴和y轴的元素数量相同,请尝试在保存图之前添加ax.set\u aspect(1)
call@user496713. 我使用的是matplotlib版本0.99.2,使用的是UBUNTU操作系统10.10。我已经添加了ax.set_特性,但是图形被修剪掉了。谢谢。@user496713。我设法更新了matplotlib。这张图表画得很漂亮。非常感谢你。好极了杰出的干杯。我只是注意到除了对角线之外,其他颜色和值都不匹配。我编辑了我的答案来解决这个问题。你能接受我的答案吗?scikit学习文档中有一个很好的功能:正如已经指出的,现在可以使用scikit的内置绘图功能,如下所示:不是答案本身,但是在这个matplotlib教程中有一些相关的例子:用户包含了相关的代码,并且为此付出了很多努力,代码是相关的,而且很好。你被其他人否决了,因为你的英语语法不够好,无法在stackoverflow上赢得声誉。对将来来说,让你的单词通过英语拼写和语法检查是个好主意。好的,G5W,不客气。我试图传播这一点,因为我真的没有发现类似的东西,我知道有很多人需要..如果有人面临绘图未显示的问题,那么有必要在最后调用matplotlib:导入matplotlib.pyplot作为plt;plt.show()
import numpy as np
import matplotlib.pyplot as plt
conf_arr = [[33,2,0,0,0,0,0,0,0,1,3],
[3,31,0,0,0,0,0,0,0,0,0],
[0,4,41,0,0,0,0,0,0,0,1],
[0,1,0,30,0,6,0,0,0,0,1],
[0,0,0,0,38,10,0,0,0,0,0],
[0,0,0,3,1,39,0,0,0,0,4],
[0,2,2,0,4,1,31,0,0,0,2],
[0,1,0,0,0,0,0,36,0,2,0],
[0,0,0,0,0,0,1,5,37,5,1],
[3,0,0,0,0,0,0,0,0,39,0],
[0,0,0,0,0,0,0,0,0,0,38]]
norm_conf = []
for i in conf_arr:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.jet,
interpolation='nearest')
width, height = conf_arr.shape
for x in xrange(width):
for y in xrange(height):
ax.annotate(str(conf_arr[x][y]), xy=(y, x),
horizontalalignment='center',
verticalalignment='center')
cb = fig.colorbar(res)
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
plt.xticks(range(width), alphabet[:width])
plt.yticks(range(height), alphabet[:height])
plt.savefig('confusion_matrix.png', format='png')
from string import ascii_uppercase
from pandas import DataFrame
import numpy as np
import seaborn as sn
from sklearn.metrics import confusion_matrix
y_test = np.array([1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5])
predic = np.array([1,2,4,3,5, 1,2,4,3,5, 1,2,3,4,4])
columns = ['class %s' %(i) for i in list(ascii_uppercase)[0:len(np.unique(y_test))]]
confm = confusion_matrix(y_test, predic)
df_cm = DataFrame(confm, index=columns, columns=columns)
ax = sn.heatmap(df_cm, cmap='Oranges', annot=True)
import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
#Assumming that your predicted results are in csv. If not, you can still modify the example to suit your requirements
df = pd.read_csv("dataframe.csv", index_col=0)
cnf_matrix = confusion_matrix(df["actual_class_num"], df["predicted_class_num"])
#getting the unique class text based on actual numerically represented classes
unique_class_df = df.drop_duplicates(['actual_class_num','actual_class_text']).sort_values("actual_class_num")
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=unique_class_df["actual_class_text"],
title='Confusion matrix, without normalization')