Python 使用matshow绘图时不完整的混淆矩阵
我正试图绘制这个confusión矩阵:Python 使用matshow绘图时不完整的混淆矩阵,python,matplotlib,jupyter-notebook,confusion-matrix,Python,Matplotlib,Jupyter Notebook,Confusion Matrix,我正试图绘制这个confusión矩阵: [[25940 2141 84 19 3 0 0 1 184 4] [ 3525 6357 322 41 5 1 3 0 242 2] [ 410 1484 1021 80 5 6 0 0 282 0] [ 98 285 189 334 9 9
[[25940 2141 84 19 3 0 0 1 184 4]
[ 3525 6357 322 41 5 1 3 0 242 2]
[ 410 1484 1021 80 5 6 0 0 282 0]
[ 98 285 189 334 9 9 5 1 140 0]
[ 26 64 55 50 112 15 4 1 75 0]
[ 11 45 20 24 5 118 8 0 79 0]
[ 1 8 8 5 0 10 62 1 55 0]
[ 2 0 0 0 0 0 2 0 6 0]
[ 510 524 103 55 5 7 7 1 65350 0]
[ 62 13 2 1 0 0 1 0 11 13]]
因此,10x10。这10个标签是:
[ 5 6 7 8 9 10 11 12 14 15]
我使用以下代码:
获取混淆矩阵
我只得到一个标签从5到9的图:
这里有什么问题
相关导入和配置(顺便说一句,我正在与Jupyter合作):
我尝试降级到matplotlib 3.1.0,因为我读到seaborn在3.1.1中出现了一些错误,但不管怎样,结果都是一样的(如果我将样式更改为ggplot也一样)。matplotlib不会在每个刻度上都添加标签(以防止刻度重叠,以免它们变长)。您可以使用
ax.set_xticks(范围(10))
在每一列强制打勾
下面是一些示例代码,其中的调用适用于matplotlib的接口。另外,一些额外的填充可以防止标题不与顶部的刻度标签一起反弹。请注意,标签可以是数字形式,matplotlib会自动将其解释为相应的字符串ax.tick_params()
可以帮助删除底部和顶部的记号标记(或者,也可以将它们放在左侧和/或右侧)。示例代码还使用次要Xtick上的网格进行分隔
导入matplotlib.pyplot作为plt
从matplotlib.ticker导入MultipleLocator
将numpy作为np导入
cm=np.random.randint(0,25000,(10,10))*np.random.randint(0,2,(10,10))
labels=np.array([5,6,7,8,9,10,11,12,14,15])
图,ax=plt.子批次()
mat=ax.matshow(厘米,插值=‘最近’)
材料集(0,100)
ax.set_xticks(范围(10))
ax.设置锁定(范围(10))
ax.set_xticklabel(标签)
ax.设置标签(标签)
ax.tick_参数(axis='x',which='bottom',bottom=False,top=False)
ax.grid(b=False,其中='major',axis='both')
ax.xaxis.set_minor_定位器(多路传送器(0.5))
ax.yaxis.set_minor_定位器(多路传送器(0.5))
ax.grid(b=True,其中='minor',axis='both',lw=2,color='white')
set_title({}分类器的混淆矩阵)。格式('clf_name'),pad=20)
plt.colorbar(mat,extend='tware')
plt.show()
cm = confusion_matrix(y_test, y_pred, labels=labels)
print('Confusion Matrix of {} is:\n{}'.format(clf_name, cm))
print(labels)
plt.matshow(cm, interpolation='nearest')
ax = plt.gca()
ax.set_xticklabels([''] + labels.astype(str).tolist())
ax.set_yticklabels([''] + labels.astype(str).tolist())
plt.title('Confusion matrix of the {} classifier'.format(clf_name))
plt.colorbar(mat, extend='both')
plt.clim(0, 100)
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
plt.style.use('seaborn')
mpl.rcParams['figure.figsize'] = 8, 6